Can't access Gmail Drafts via Gmail Add-ons

2019-05-18 20:51发布

问题:

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"
}
}

回答1:

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