Gmail API users.watch - 没有historyId的详细信息

时间:2015-08-03 21:55:32

标签: google-api gmail-api google-cloud-pubsub

我已成功设置Google Pub / Sub以使用此处所述的Gmail API Watch功能:https://developers.google.com/gmail/api/guides/push在我的Gmail帐户中观看INBOX标签。

一旦有新消息到达,我会立即获得有效格式的推送通知,如:

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

我base64decode数据后,我收到电子邮件和historyId。然后,按照建议,我请求gmail.users.history.list API(通过API控制台),并将startHistoryId设置为推送通知中的historyId。然后在没有任何细节的情况下获得空响应:

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

因此,来自通知的historyId似乎无效。 似乎Gmail用户。手表API工作不正常,发送错误的historyId,或者我只是遗漏了什么?

1 个答案:

答案 0 :(得分:18)

看起来我误解了users.history.list是如何工作的,流程应该如下所述:

  1. 记住historyId来自users.watch请求的响应。

  2. 在推送通知中,调用users.history.list,将之前记住的historyId作为startHistoryId而不是来自通知的新记录,并获取最近更改的列表。

  3. 记住historyId来自通知,并转到2。

  4. 如果我们查看对users.history.list的任何调用的响应,则historyId始终存在,它是最新历史记录更改的标记。推送通知带来了最新的historyId,所以如果我们用它请求users.history.list(如问题中所示),我们得到空的历史数组,服务器就会删除这个空的"历史"响应中的字段,这就是我们得到这个的原因:

    {
     "historyId": "4658894"
    }
    

    但不是这样:

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

    同步指南中提供了更多详细信息:https://developers.google.com/gmail/api/guides/sync#partial

    因此,我们无法轻松获取从推送通知到达INBOX的新消息的详细信息,并且需要处理历史记录挖掘和同步才能找到它。