Reputation: 1103
I'm using the example here to guide me as I write code to compose a standalone draft email using user input values from my gmail addon.
Here's the button widget that should create the draft:
var submitButton = CardService.newTextButton()
.setTextButtonStyle(CardService.TextButtonStyle.FILLED)
.setText('Create Draft')
.setComposeAction(
CardService.newAction().setFunctionName("createEmailDraft"),
CardService.ComposedEmailType.STANDALONE_DRAFT
);
And here's the callback:
function createEmailDraft(e) {
var recipient = e.formInput.recipient;
var subject = e.formInput.subject;
var body = e.formInput.body;
var draft = GmailApp.createDraft(recipient, subject, body);
return CardService.newComposeActionResponseBuilder()
.setGmailDraft(draft).build();
}
I keep encountering the following error:
Access denied: : Missing access token for authorization. Request: MailboxService.CreateDraft.
My scopes seem alright (overly permissive if anything):
"oauthScopes": [
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/gmail.addons.current.action.compose",
"https://www.googleapis.com/auth/script.external_request"
],
I would greatly appreciate any help getting past this error!
Upvotes: 1
Views: 668
Reputation: 2107
This one is a bit deeper in the documentation, but you must use the access token in the callback Action event to authorize the draft creation:
function createEmailDraft(e) {
var accessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
var recipient = e.formInput.recipient;
var subject = e.formInput.subject;
var body = e.formInput.body;
var draft = GmailApp.createDraft(recipient, subject, body);
return CardService.newComposeActionResponseBuilder()
.setGmailDraft(draft).build();
}
Upvotes: 2