Gmail API users.watch - 没有 historyId 的详细信息 [英] Gmail API users.watch - no details for historyId

查看:16
本文介绍了Gmail API users.watch - 没有 historyId 的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功设置 Google Pub/Sub 以使用 Gmail API Watch 功能,如下所述:https://developers.google.com/gmail/api/guides/push 在我的 Gmail 帐户中查看收件箱标签.

I have successfully set up Google Pub/Sub to use Gmail API Watch feature as described here: https://developers.google.com/gmail/api/guides/push to watch INBOX label in my gmail account.

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

Once new message arrive I instantly get a push notification in valid format like:

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

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

After I base64decode data I get email and historyId. Then, as suggested, I request gmail.users.history.list API (via API console) with startHistoryId set to the historyId from the push notification. And then get just empty response 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"
}

因此通知中的 historyId 似乎无效.似乎 Gmail users.watch API 工作不正常,并且发送了错误的 historyId,或者我只是遗漏了什么?

So historyId from a notification does not seems valid. Seems Gmail users.watch API is not working properly, and sends wrong historyId, or I'm just missing something?

推荐答案

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

Looks like I misunderstood how users.history.list works, and the flow should be something like described below:

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

  1. Remember historyId from the response of users.watch request.

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

On push notification call users.history.list with previously remembered historyId as startHistoryId instead of new one from notification, and get list of recent changes.

记住通知中的 historyId,然后转到 2.

Remember historyId from notification, and goto 2.

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

If we look on the response of any call to users.history.list, historyId is always present, it's a marker of latest history change. Push notification brings latest historyId, so if we request users.history.list with it (as in the question), we got empty array of history, and the server drops this empty "history" field from the response, that's why we get this:

{
 "historyId": "4658894"
}

但不是这个:

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

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

More details provided in sync guide: https://developers.google.com/gmail/api/guides/sync#partial

所以我们不能轻易从推送通知中获取到达INBOX的新消息的详细信息,需要进行历史挖掘和同步才能找到它.

So we can't easily get details of the new message arrived to INBOX from push notification, and need to deal with history mining and synchronization to find it.

这篇关于Gmail API users.watch - 没有 historyId 的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆