Gmail REST API获取邮件功能会返回无效的historyId

jam*_*mie 4 gmail-api

好像Gmail REST API中可能存在严重问题.

  1. 调用/ userId/messages以获取消息列表
  2. 对于每个,调用/ userId/messages/id来获取消息
  3. 获取每个消息对象上的最高(或任何)startHistoryId
  4. 然后调用/ userId/history/list传递startHistoryId作为参数

结果出人意料.Gmail REST API返回404 Not Found ..似乎返回的historyId未注册或有效.

在调用/ userId/profile时,startHistoryId有效,可以在/ userId/history/list调用中成功使用.

com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
"code" : 404,
"errors" : [ {
     "domain" : "global",
     "message" : "Not Found",
     "reason" : "notFound"
    } ],
  "message" : "Not Found"
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*c D 6

这不是API中的错误,请参阅 https://developers.google.com/gmail/api/v1/reference/users/history/list

具体而言"历史ID按时间顺序增加,但不与有效ID之间的随机间隔连续.提供无效或过时的startHistoryId通常会返回HTTP 404错误代码.historyId通常有效至少一周,但在某些情况下可能只有几个小时有效.如果收到HTTP 404错误响应,您的应用程序应执行完全同步."

因此,您可能只是使用超出历史记录范围的historyId(它不会无限期存储,这将非常昂贵).它只存储足够长的时间来同步客户端需要它(例如一周左右).

如果您只需要从某个点向前同步,那么只需使用从getProfile返回的historyId.消息上的historyId是上次更新消息的时间,可能是几个月或几年前,比存储历史记录的时间长.

另请参阅同步指南:https: //developers.google.com/gmail/api/guides/sync