【问题标题】:GMail api users().history().list returns 'nextPageToken', but no 'history' field?GMail api users().history().list 返回“nextPageToken”,但没有“history”字段?
【发布时间】:2017-10-31 18:49:57
【问题描述】:

我使用 GMail API 的 history.list 来检索已更改消息的列表,这适用于几页历史记录 - 但有时当返回 nextPageToken 时,它用于检索返回的下一页没有 history 字段。没有提出HttpError

results = self.service.users ().history ().list (userId = self.account, startHistoryId = start).execute ()
if 'history' in results:
  yield results['history']

while 'nextPageToken' in results:
  pt = results['nextPageToken']
  results = self.service.users ().history ().list (userId = self.account, startHistoryId = start, pageToken = pt).execute ()
  yield results['history'] # this fails with missing 'history' member.

【问题讨论】:

  • 您是否期待某个结果但不存在?如果真的没有什么可显示的怎么办?
  • 如果没有更多要显示的内容,则不应有nextPageToken。这就是我知道我已经到达列表末尾的方式。如果我得到一个nextPageToken,但没有更多结果,没有任何错误,我不相信由于某处的一些错误而没有遗漏某些东西 - 因为这是增量更新,我可能会跳过那些丢失的更新下一次增量更新。
  • @boardrider:这种情况很少发生(在数千或数百万个请求中出现一次),完整示例在这里(示例底部):developers.google.com/gmail/api/v1/reference/users/history/list

标签: python gmail gmail-api


【解决方案1】:

如果我正确理解问题,您不需要在历史记录页面上看到任何结果。

while 'nextPageToken' in response:
    page_token = response['nextPageToken']
    response = gcon.users().messages().list(userId='me', pageToken=page_token).execute()
    if response['resultSizeEstimate'] is 0:
        break
    email.extend(response['messages'])
return email

我认为这会有所帮助。

if response['resultSizeEstimate'] is 0:
    break

【讨论】:

  • 我记不太清了,但我认为也没有resultSizeEstimate 字段。无论如何,这似乎不太可靠。不是针对整个查询吗?并且有返回结果,只是nextPageToken表示有更多结果——但没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 2012-03-13
  • 1970-01-01
  • 2017-05-02
  • 2016-03-01
  • 1970-01-01
相关资源
最近更新 更多