2017-10-15 107 views
1

我已启用Gmail API,并在stackoverflow上查找所有问题以确保我不会丢失任何内容。即使符合所有的选项gmail的API后,我收到以下错误:应用程序脚本中的gmail api错误+权限不足错误代码403域全局

{ 
"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()); 
+0

你在哪里实例化'ScriptApp'?我怀疑你只需要https://www.googleapis.com/auth/gmail.labels这个范围,并且你至少需要https://www.googleapis.com/auth/gmail.send来发送一个消息。 – Tholle

+0

如何在应用程序脚本中添加其他范围?我在高级的谷歌服务中启动了Gmail API,并且启用了Gmail项目的API。 – MSKK

+0

对不起,它应该可能在配置后自动执行,我的不好。你有没有偶然发现[**这个答案**](https://stackoverflow.com/questions/33620359/insufficient-permissions-error-with-google-appscripts#answer-33660755)? – Tholle

回答

0

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

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

希望它有帮助。

+0

“enabledAdvancedServices”:[{ “userSymbol”: “Gmail的”, “服务Id”: “Gmail的”, “版本”: “V1” },{ “userSymbol “: “幻灯片”, “服务Id”: “幻灯片”, “版本”: “V1” },{ “userSymbol”: “驱动器”, “服务Id”: “驱动器”, “版本”: “V2” },{ “userSymbol”: “加”, “服务Id”: “加”, “版本”: “V1” },{ “userSymbol”: “表”, “SE rviceId“:”sheets“, ”version“:”v4“ }], – MSKK

+0

这是我在清单文件中看到的...清单文件中的哪个位置添加此范围? – MSKK