I am trying to access all the drafts in Gmail using Gmail Add-ons, but it logs error like this
Access denied: : Missing access token for per message scope authorization. [line: 8, function: buildAddon, file: Code]
My onTrigger function looks like this
function buildAddon(e) {
var cards = [];
var accessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
var draftMessages = GmailApp.getDraftMessages();
for (var i = 0; i < draftMessages.length; i++) {
cards.push(CardService.newCardBuilder()
.setHeader(CardService.newCardHeader()
.setTitle(draftMessages[i].getBody())).build());
}
return cards;
}
And the manifest json
file looks like this
{
"oauthScopes": [
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/gmail.addons.current.message.action",
"https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
"https://www.googleapis.com/auth/gmail.addons.current.message.readonly",
"https://www.googleapis.com/auth/gmail.addons.current.action.compose"
],
"gmail": {
"contextualTriggers": [
{
"unconditional": {},
"onTriggerFunction": "buildAddon"
}
],
"logoUrl": "https://www.gstatic.com/images/icons/material/system/2x/bookmark_black_24dp.png",
"name": "Draft reminder",
"version": "TRUSTED_TESTER_V2"
}
}
You’re missing the scope for drafts permission.
“https://www.googleapis.com/auth/gmail.compose Manage drafts and send email”
Here is the reference for scopes: https://developers.google.com/identity/protocols/googlescopes#gmailv1
来源:https://stackoverflow.com/questions/47225189/cant-access-gmail-drafts-via-gmail-add-ons