Gmail.Users.Threads.get,元数据范围和元数据格式不起作用

时间:2018-04-17 17:15:48

标签: google-apps-script gmail-api

我确定这是一个非常愚蠢的事情,我很想念,但我看不到它。 我的Google Apps脚本只需要邮件标题,因此它的范围非常严格: “https://www.googleapis.com/auth/gmail.metadata”。我真的不想改变这个范围,因为它提供了我需要的东西。

由于此限制范围,许多API调用将强制您选择METADATA格式而不是默认的FULL。可以使用API​​资源管理器(https://developers.google.com/gmail/api/v1/reference/users/threads/get)来检查。记得选择JUST元数据范围,否则它会起作用! : - )

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Metadata scope doesn't allow format FULL"
   }
  ],
  "code": 403,
  "message": "Metadata scope doesn't allow format FULL"
 }
}

然后,从格式下拉菜单中选择“元数据”再次运行,它将起作用。

这个简单的代码足以复制问题:

Code.gs

function getThread() {
  // get the most recent thread
  var thread = Gmail.Users.Threads.list('me', {maxResults: 1});
  Logger.log('thread: %s', thread);
  thread = JSON.parse(thread);
  var threadId = thread.threads[0].id;
  Logger.log('threadId: %s', threadId);
  // scope "https://www.googleapis.com/auth/gmail.metadata"
  // requires me to specify the metadata format
  // next line will error: Metadata scope doesn't allow format FULL
  var thread = Gmail.Users.Threads.get('me', {id: threadId, format: 'metadata'}); 
}

appsscript.json(查看 - >显示清单文件)

{
  "oauthScopes": ["https://www.googleapis.com/auth/gmail.metadata"],
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Gmail",
      "serviceId": "gmail",
      "version": "v1"
    }]
  },
  "exceptionLogging": "STACKDRIVER"
}

运行getThread()函数时,会产生以下错误:

Metadata scope doesn't allow format FULL (line 10, file "Code.gs")

Gmail API,特别是Gmail.Users.Thread.get文档(https://developers.google.com/gmail/api/v1/reference/users/threads/get)声明:

可选查询参数 format string The format to return the messages in.

可接受的值是:

  • “full”:返回有效内容字段中已解析的电子邮件内容,并且不使用原始字段。 (默认)
  • “metadata”:返回带有邮件元数据的电子邮件标头,例如标识符和标签。
  • “minimal”:仅返回标识符和标签等电子邮件元数据,不返回电子邮件标题,正文或有效负载。

所以我不清楚应该如何编写这个API调用:

var thread = Gmail.Users.Threads.get('me', {id: threadId, format: 'metadata'});

我已经尝试了所有可能的引号组合(单引号,双引号和无引号)与case(upper和lower)并没有任何效果。它似乎被忽略了......: - (

我很难过......请帮忙! : - )

谢谢!

1 个答案:

答案 0 :(得分:0)

根据API文档,Gmail.Users.Threads.get()需要2个参数,并且有一个可选的2(您指定的格式对象):

  

路径参数
   id string 要检索的主题的ID    userId 字符串用户的电子邮件地址。特殊值me可用于指示经过身份验证的用户。

     

可选查询参数
  格式 字符串返回消息的格式。可接受的值为:
    - " full":返回有效内容字段中已解析的电子邮件内容,并且不使用原始字段。 (默认)
    - " metadata":返回带有标识符和标签等消息元数据的电子邮件标题     - " minimal":仅返回电子邮件元数据(如标识符和标签),它不会返回电子邮件标题,正文或有效负载。
   metadataHeaders [] list 如果给定且格式为METADATA,则只包含指定的标头。

在Apps脚本的高级服务Gmail客户端库中,提供了2个签名: enter image description here

此处的参数应为:

  1. userId - 要求的用户(针对您的情况,"me"
  2. id - 您要获取的主题的ID
  3. 您的可选参数对象