简体   繁体   中英

Google Apps Scripts — Using getdraft() method not working

Trying to extend the compose UI to get some information from drafts, When I try to get the draft using draft.getMessage().getBody(), I have this error:

Access denied: : Missing access token for authorization. Request: MailboxService.GetMessage. [line: 19, function: validateRecipients, file: Code]

My code:

var draft = GmailApp.getDrafts()[0];
var content = draft.getMessage().getBody();
draft.update("blabla@gmail.com", "Disclosure Alert", "The below message is flagged as possible data disclosure.\n Recipients:"+allEmails+"\n Content:\n"+content);
draft.send();

My Manifest Scopes:

  "oauthScopes" : [
    "https://mail.google.com/",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/script.locale",
    "https://www.googleapis.com/auth/script.send_mail",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/gmail.metadata",
    "https://www.googleapis.com/auth/gmail.modify",
    "https://www.googleapis.com/auth/gmail.compose",
    "https://www.googleapis.com/auth/gmail.send",
    "https://www.googleapis.com/auth/gmail.addons.current.action.compose", 
    "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.message.action",
    "https://www.googleapis.com/auth/gmail.addons.execute"
    ]

When using multiple scopes in an add-on, the most restrictive scopes possible will be automatically selected (See related question: In Gmail Addons, how to get access to all threads' details returned by GmailApp.search? ).

Since you are already using the least restrictive GMail scope ( https://mail.google.com/ ) I suggest you change your manifest scopes to the following (Notice removed https://www.googleapis.com/auth/gmail.addons.current.message.* scopes):

"oauthScopes" : [
    "https://mail.google.com/",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/script.locale",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/gmail.addons.execute"
]

If you consider publishing this add-on, bear in mind that you will not be allowed the https://mail.google.com scope, so you may be interested in checking out other scopes such as https://www.googleapis.com/auth/gmail.readonly .

Reference

Under each function/method on app script doc you got the Scopes coming with. For this one:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM