【问题标题】:GMail API Users.threads.list Missing "Messages" FieldGMail API Users.threads.list 缺少“消息”字段
【发布时间】:2017-01-19 04:57:45
【问题描述】:

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

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

结果如下,有效,生成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[] 数组!我知道字段掩码是有效的,因为当您请求该方法不支持标准 HTTP 400 和漂亮打印错误消息的字段时,GMail API 会出错。但在这种情况下,我得到的只是一个包含标记为“线程”的数组的对象,其中每个线程对象只有以下字段:

  • 身份证
  • sn-p
  • historyId

但没有消息数组。更奇怪的是,即使我删除了fields 自定义过滤器参数,我仍然得到相同的结果。我在这里遗漏了什么明显的东西吗?

【问题讨论】:

    标签: javascript api gmail-api


    【解决方案1】:

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

    1。列出 ID

    请求

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

    回应

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

    2。获取线程

    请求

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

    回应

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-30
      • 2012-01-03
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多