获取特定 headers 以及 Users.thread 中的其他 body 数据:获取

Getting specific headers along with other body data in Users.thread: get

你可以很容易地 select 你想要什么 headers Users.thread: get-request, 像这样:

format = metadata
metadataHeaders = From

GET https://www.googleapis.com/gmail/v1/users/me/threads/14eaffaf5e3e8242?metadataHeaders=From&format=metadata&key={YOUR_API_KEY}

响应:

{
 "id": "14eaffaf5e3e8242",
 "historyId": "510358",
 "messages": [
  {
   "id": "14eaffaf5e3e8242",
   "threadId": "14eaffaf5e3e8242",
   "labelIds": [
    "SENT",
    "INBOX",
    "IMPORTANT"
   ],
   "snippet": "Wow Emil!",
   "historyId": "510292",
   "internalDate": "1437471536000",
   "payload": {
    "mimeType": "multipart/mixed",
    "headers": [
     {
      "name": "From", // I just got the header I asked for.
      "value": "Emil Tholin <emtholin@gmail.com>"
     }
    ]
   },
   "sizeEstimate": 9260
  }, ...
}

您还可以非常轻松地获得 body 的某些部分。例如。在这里,我要求消息中所有附件的 attachmentIds body:

field = messages/id,messages/payload/parts/body/attachmentId

GET https://www.googleapis.com/gmail/v1/users/me/threads/14eaffaf5e3e8242?fields=messages%2Fid%2Cmessages%2Fpayload%2Fparts%2Fbody%2FattachmentId&key={YOUR_API_KEY}

回复:

{
 "messages": [
  {
   "id": "14eaffaf5e3e8242",
   "payload": {
    "parts": [
     {
      "body": {  // This message had an attachment.
       "attachmentId": "ANGjdJ_0lphTo48BO0xBT_YOSo3tYah23hzpjyATe3GwfziK0I6401P_8-ZYoGuCQPHhpPP0-S_pjL68WIEZzQ0tu72RcIOE4UY3kA4u8PjXPf3Cm5PxVJjmH9N0hm0fFX31RYo8bfZQ6l7bDbYbnCSZbckG7g8enGaKMPbBzIEEC4HXr_YghOYWSfrXKXiFLnxWN4LfsFk3IXUN2tVvMe_0xMhDDfBlqYPnXHr2PhPghq7bQojNxiH4YziIqaKmwiU4xqVfygbae-K-_Q2blyz0EgI4OXjMzwz56Q5e1w"
      }
     }
    ]
   }
  },
  {
   "id": "14eaffb277b61cd0" // This message had no attachment.
  }, ...
 ]
}

正如您在第一个请求中看到的,在请求特定元数据 headers 时,body 的任何部分都没有被检索到。 fields 参数中的各个字段也很难挑选出来,因为 headers 不是 key-value 对,而是 { "name": <HEADER_NAME>, "value": <HEADER_VALUE> } 形式的 objects。

有什么办法可以把这两个请求结合起来吗?我想在获取邮件中有多少附件的同时获取有关邮件的所有相关元数据。

来自 Users.threads.get.

"metadata": Returns email headers with message metadata such as identifiers and labels.

fields参数仅用于限制返回的数据量。由于 metadata 模式已经是一个有限的响应,您不能使用字段来获取该子集之外的数据。您最好的选择是使用带有字段的第二个示例,然后在本地过滤元数据值。