首页 文章

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

提问于
浏览
4

我'm able to use GMail' s Users.threads.list API调用来检索线程列表 . 我还想 grab 属于线程中每条消息的 labels .

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

enter image description here

这导致以下有效的生成 fields 参数值:

nextPageToken,resultSizeEstimate,threads(historyId,id,messages / labelIds)

最终请求如下所示:

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

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

  • id

  • 片段

  • historyId

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

1 回答

  • 1

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

    1.列表ID

    Request

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

    Response

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

    2.获取主题

    Request

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

    Response

    {
     "id": "1570f8c16a1084de",
     "historyId": "1234",
     "messages": [
      {
       "id": "1570f8c16a1084de",
       "threadId": "1570f8c16a1084de",
       "labelIds": [
        "INBOX",
        "IMPORTANT",
        "CATEGORY_FORUMS"
        ], ...
       }
      }
    

    如果您不想列出线程然后单独获取它们,则可以使用batch requests将其降低到2个请求 .

相关问题