Gmail API users.watch - no data for historyId

I have successfully configured Google Pub / Sub to use the Gmail API viewer, as described here: https://developers.google.com/gmail/api/guides/push to view the INBOX shortcut in my gmail.

When a new message arrives, I instantly receive a push notification in a valid format, for example:

{ message: { data: '.......', attributes: {}, message_id: '1248700053943' }, subscription: '.....' } 

After I received the base64decode data, I received the email and historyId. Then, as suggested, I request the gmail.users.history.list API (via the API console) using startHistoryId set to historyId from the push notification. And then you get only an empty answer without any details:

 GET https://www.googleapis.com/gmail/v1/users/me/history?startHistoryId=4658879&key={YOUR_API_KEY} 200 OK - Show headers { "historyId": "4658894" } 

Therefore, the historyId from the notification does not appear to be valid. It looks like the Gmail users.watch API is not working correctly and sending the wrong ID history, or am I just missing something?

+8
google-api gmail-api google-cloud-pubsub
source share
1 answer

It looks like I misunderstood how user.history.list works, and the stream should be something like this as described below:

  • Remember historyId from the answer to users.watch.

  • In push notification mode, users.history.list is called with a history previously remembered as startHistoryId, rather than a new notification, and receives a list of recent changes.

  • Remember the notification story and goto 2.

If we look at the response of any call to users.history.list, historyId is always present, this is a marker of the last change in history. The push notification brings the latest history, so if we request user.history.list from it (as in the question), we get an empty history array, and the server discards this empty history field from the answer, so we get this

 { "historyId": "4658894" } 

But not this:

 { "history": [ ], "historyId": "4658894" } 

Learn more in the sync guide: https://developers.google.com/gmail/api/guides/sync#partial

Thus, we cannot easily get information about a new message received in INBOX from a push notification, and to find it we need to deal with history mining and synchronization.

+13
source share