Yar*_*yak 10 google-api gmail-api google-cloud-pubsub
我已成功设置Google Pub/Sub以使用Gmail API监视功能,如下所述:https://developers.google.com/gmail/api/guides/push在我的Gmail帐户中观看INBOX标签.
一旦收到新消息,我立即以有效格式获得推送通知,如:
{ message:
{ data: '.......',
attributes: {},
message_id: '1248700053943' },
subscription: '.....' }
Run Code Online (Sandbox Code Playgroud)
在我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"
}
Run Code Online (Sandbox Code Playgroud)
因此,来自通知的historyId似乎无效.似乎Gmail users.watch API工作不正常,发送错误的historyId,或者我只是遗漏了什么?
Yar*_*yak 18
看起来我误解了users.history.list是如何工作的,流程应该如下所述:
记住historyId来自users.watch请求的响应.
在推送通知中,调用users.history.list,将之前记住的historyId作为startHistoryId而不是来自通知的新建,并获取最近更改的列表.
记住historyId来自通知,并转到2.
如果我们查看对users.history.list的任何调用的响应,则historyId始终存在,它是最新历史记录更改的标记.推送通知带来了最新的historyId,所以如果我们用它请求users.history.list(如问题所示),我们得到空的历史数组,服务器从响应中删除这个空的"历史"字段,这就是为什么我们得到这个:
{
"historyId": "4658894"
}
Run Code Online (Sandbox Code Playgroud)
但不是这个:
{
"history": [ ],
"historyId": "4658894"
}
Run Code Online (Sandbox Code Playgroud)
同步指南中提供了更多详细信息:https://developers.google.com/gmail/api/guides/sync#partial
因此我们无法轻易获得从推送通知到达INBOX的新消息的详细信息,并且需要处理历史挖掘和同步才能找到它.
归档时间: |
|
查看次数: |
2600 次 |
最近记录: |