应用程序脚本中的gmail api错误+权限不足错误代码403域全局

时间:2017-10-15 22:15:06

标签: google-apps-script gmail-api

我启用了Gmail API并查找了有关stackoverflow的所有问题,以确保我没有遗漏任何内容。即使在符合gmail API的所有选项后,我也会收到以下错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}

我没有选择,想知道我哪里出错了。

    var body = "<html><body>"
      + "<p>" + "Hi Team," + "<br/><br/>"
      + "<p>" + "Can you please review the bid for the following opportunity:" + "<br/>" 
      + "<p>" + "<u><b>Opportunity Details: </b></u>"+ "<br/>"  
      + "</body></html>";

      Logger.log(body);  
      var con = ContactsApp.getContactsByGroup(ContactsApp.getContactGroup('Australia')); 
      var toList = [];
      for (var i in con) {
        toList[i] = "<" + con[i].getPrimaryEmail() + ">";  
      }  
      toList = toList.join(',');

      var msg = {
        to: toList,
        from: {
          name: ContactsApp.getContact(Session.getEffectiveUser().getEmail()).getFullName(),
          email: Session.getEffectiveUser().getEmail(),
          signature: Gmail.Users.Settings.SendAs.list("me").sendAs[0].signature
        },
        body: body,
        subject: sub
      };

      //  var htmlBody = '<p>Hello, I am an HTML message</p><br/>' + '<a href="---------------">Click here</a>';
      //  var signature = Gmail.Users.Settings.SendAs.list("me").sendAs[0].signature    ;    //sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;

      var raw = 'From: <' + msg.from.email + '>\r\n' +
        'To: ' + msg.to + '\r\n' +
          'Subject:' + msg.subject + '\r\n' +
            'Content-Type: text/html; charset=UTF-8\r\n' + 
              '\r\n' + msg.body + msg.from.signature;

      var draftBody = Utilities.base64Encode(raw, Utilities.Charset.UTF_8).replace(/\//g,'_').replace(/\+/g,'-');

      var params = {
        method      : "post",
        contentType : "application/json",
        headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
        devMode     : true,
        muteHttpExceptions:true,
        payload:JSON.stringify({
          "message": {
            "raw": draftBody
          }
        })
      };

      var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts", params);
      Logger.log(resp);
      Logger.log(resp.getContentText());

1 个答案:

答案 0 :(得分:0)

我通过手动修改Google Scripts界面中的清单文件解决了这个问题。

如果您查看文件&gt;项目属性&gt;范围,您会发现应用程序已经在某些范围内请求授权。您可以通过更改查看&gt;下的json清单文件来手动添加到这些范围。显示清单文件,在项目属性+中添加Gmail API所需的文件,在我的案例中为“https://www.googleapis.com/auth/gmail.compose”。

希望它有所帮助。