GMail API Users.threads.list缺少“消息”字段

时间:2016-09-11 17:22:21

标签: javascript api gmail-api

我可以使用GMail的 Users.threads.list API调用来检索线程列表。我还想抓住线程中每条消息的标签

在这个方法的official documentation / live example上,有一个名为fields的区域,用户可以在其中指定他们希望输入的可选字段。他们甚至提供一些UI工具调用'字段编辑器'帮助您选择并正确格式化字段以包含在REST查询中:

enter image description here

结果如下,有效,生成fields参数值:

  

nextPageToken这个,resultSizeEstimate,线程(historyId,ID,消息/ labelIds)

最终请求如下:

GET https://www.googleapis.com/gmail/v1/users/me/threads?includeSpamTrash=true&fields=nextPageToken%2CresultSizeEstimate%2Cthreads&key={YOUR_API_KEY}

使用OAuth2进行身份验证后,我会返回threads[]的列表,但 none 包含嵌套在其中的预期messages[]数组!我知道字段掩码是有效的,因为当您请求字段时,GMail API会出错,该方法不支持标准HTTP 400和漂亮的打印错误消息。但在这种情况下,我得到的只是一个包含标记为“threads”的数组的对象,其中每个线程对象只有以下字段:

  • ID
  • historyId

但没有消息数组。更奇怪的是,即使我删除了fields自定义过滤器参数,我仍然会收到这些相同的结果。我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:1)

您必须先list the id of the threads,然后向get the thread and fields in the messages you want单独提出请求。 API Explorer允许您在列出时出于某种原因选择其他字段。

1。列表ID

请求

>>> import pandas
>>> s = pandas.Series(range(4), index=['a', 'b', 'c', 'd'])
>>> s['b':'d']
b    1
c    2
d    3
dtype: int64

<强>响应

GET https://www.googleapis.com/gmail/v1/users/me/threads?access_token={access_token}

2。获取线程

请求

{
 "threads": [
  {
   "id": "1570f8c16a1084de",
   "snippet": "Foo bar...",
   "historyId": "1234"
  }, ...
 ],
 "resultSizeEstimate": 100
}

<强>响应

GET https://www.googleapis.com/gmail/v1/users/me/threads/1570f8c16a1084de?access_token={access_token}

如果您不想列出线程然后单独获取它们,则可以使用batch requests将其归结为2个请求。