I am trying to execute a watch
to enable GMail push API to send notifications to PubSub. I am using Elixir, and I am already using others Google API, in the same language; for some reason, I fail with GMail API.
I am receiving a 400 BAD REQUEST, with "preconditionFailed" in the body.
Since the 400, I guessed that I was passing the value in the wrong way. So I tried to get the list of the messages, which is a call that takes no parameters:
{:ok, token} = Goth.Token.for_scope("https://mail.google.com/")
conn = GoogleApi.Gmail.V1.Connection.new(token.token)
GoogleApi.Gmail.V1.Api.Users.gmail_users_messages_list(conn, "me")
And still I receive a 400:
{:error,
%Tesla.Env{
__client__: %Tesla.Client{
adapter: nil,
fun: nil,
post: [],
pre: [
{Tesla.Middleware.Headers, :call,
[
[
{"authorization",
"Bearer TOKEN HERE"}
]
]}
]
},
__module__: GoogleApi.Gmail.V1.Connection,
body: "{\n \"error\": {\n \"errors\": [\n {\n \"domain\": \"global\",\n \"reason\": \"failedPrecondition\",\n \"message\": \"Bad Request\"\n }\n ],\n \"code\": 400,\n \"message\": \"Bad Request\"\n }\n}\n",
headers: [
{"cache-control", "private, max-age=0"},
{"date", "Fri, 01 May 2020 19:40:10 GMT"},
{"accept-ranges", "none"},
{"server", "GSE"},
{"vary", "X-Origin"},
{"content-length", "179"},
{"content-type", "application/json; charset=UTF-8"},
{"expires", "Fri, 01 May 2020 19:40:10 GMT"},
{"x-content-type-options", "nosniff"},
{"x-frame-options", "SAMEORIGIN"},
{"content-security-policy", "frame-ancestors 'self'"},
{"x-xss-protection", "1; mode=block"},
{"alt-svc",
"h3-Q050=\":443\"; ma=2592000,h3-Q049=\":443\"; ma=2592000,h3-Q048=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\""}
],
method: :get,
opts: [],
query: [],
status: 400,
url: "https://www.googleapis.com/gmail/v1/users/me/messages"
}}
I don't understand why this is failing. Also, I have checked with REST specs, and it looks correct:
https://developers.google.com/gmail/api/v1/reference/users/messages/list
P.s.: as a reference, the following code works:
{:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform")
conn = GoogleApi.PubSub.V1.Connection.new(token.token)
pubsub_message = %GoogleApi.PubSub.V1.Model.PubsubMessage{
data: message |> Jason.encode!() |> Base.encode64(padding: true)
}
request = %GoogleApi.PubSub.V1.Model.PublishRequest{
messages: [pubsub_message]
}
Projects.pubsub_projects_topics_publish(
conn,
<PROJECT>,
"consolidation-messages",
body: request
)
So the authentication procedure looks correct.