我已啓用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());
你在哪裏實例化'ScriptApp'?我懷疑你只需要https://www.googleapis.com/auth/gmail.labels這個範圍,並且你至少需要https://www.googleapis.com/auth/gmail.send來發送一個消息。 – Tholle
如何在應用程序腳本中添加其他範圍?我在高級的谷歌服務中啓動了Gmail API,並且啓用了Gmail項目的API。 – MSKK
對不起,它應該可能在配置後自動執行,我的不好。你有沒有偶然發現[**這個答案**](https://stackoverflow.com/questions/33620359/insufficient-permissions-error-with-google-appscripts#answer-33660755)? – Tholle