RTF
RTF

Reputation: 6484

Unable to retrieve message content after Google Pub/Sub webhook call to server

I've subscribed to Google Cloud Pub/Sub, created a topic and created a subscription with a webhook that points to a URL at my server. I've then used the Gmail API to call watch on a particular label that I've created in my Gmail account and associated it with the topic I created.

When I messages arrive into the label in my Gmail account, the webhook is triggered a POST request to the URL at my server. But no matter what the content of the email is, the POST body is always something like this:

{
    "message": {
        "attributes": {},
        "data": "eyJlbWFpbEFkZHJlc3MiOiJteWVtYWlsQG15ZG9tYWluLmNvbSIsImhpc3RvcnlJZCI6MTIzNDU2N30K",
        "messageId": "12345678900000", # only relevant to Pub/Sub
        "message_id": "12345678900000",
        "publishTime": "2017-04-16T15:42:08.968Z",
        "publish_time": "2017-04-16T15:42:08.968Z"
    },
    "subscription": "projects/proj-name/subscriptions/sub-name"
} 

The data field is Base64 encoded, which in this case is:

{"emailAddress":"[email protected]","historyId":1234567}

That's what I get, every time, no matter what the email content is. However, if run a publish test from the google developer console, the base64 encoded value in the data field is the actual message string that I specified.

I've tried making a subsequent call to history.list with the Gmail API using the historyId from the base64 decoded data value, but all I get back is a response like this:

{"historyId": "1234567"}

How am I suppose to get the email content?

Upvotes: 6

Views: 2072

Answers (1)

jboga
jboga

Reputation: 199

A bit late but others might be interested. The answer is here. Basically you get an historyId from the watch call. Store it. Then when you get a notification from Gmail:

  • keep track of the historyId
  • fetch the history list (gmail.users.history.list) using the stored historyId (from the watch call)
  • import all the desired messages from the history
  • once you're done, replace the current historyId by the one from the notification.

Hope it helps.

Upvotes: 7

Related Questions