2

I'm running my gmail add-on with the following scopes:

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/gmail.addons.current.message.metadata
https://www.googleapis.com/auth/script.external_request

I have a button, and corresponding compose action event, that is supposed to read recipient, subject, and body information from some input fields and insert this info into the compose UI.

The compose action callback looks like this:

function autofillEmailDraft(e) {
  var recipient = e.formInput.recipient;
  var subject = e.formInput.subject;
  var body = e.formInput.body;

  GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken);
  var draft = GmailApp.createDraft(recipient, subject, body);

  // Return a built draft response. This causes Gmail to present a
  // compose window to the user, pre-filled with the content specified
  // above.
  return CardService.newComposeActionResponseBuilder()
    .setGmailDraft(draft).build();
}

On clicking the button, the addon crashes with the following error message: TypeError: Cannot read property "accessToken" from undefined.

e.messageMetadata seems to be undefined. Should I be looking for the access token somewhere else?

Simply removing the offending line

GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken);

doesn't work. The add-on crashes with a different error

Access denied: : Missing access token for authorization. Request: MailboxService.CreateDraft.

Update after comments:

This is the code for the button widget that triggers the compose action:

var submitButton = CardService.newTextButton()
      .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
      .setText('Yes')
      .setComposeAction(
        CardService.newAction().setFunctionName('autofillEmailDraft'),
        CardService.ComposedEmailType.STANDALONE_DRAFT
      );

This is the logged 'e' object:

{
    formInput={body=Hello},
    parameters={},
    draftMetadata={
        toRecipients=[abcd@example.com],
        bccRecipients=[],
        ccRecipients=[]
    }
}
ack_inc
  • 1,015
  • 7
  • 13
  • 1
    `e` needs to be an [`Action`](https://developers.google.com/gmail/add-ons/concepts/actions#action_event_objects) object. How is this function being triggered? You can try `Logger.log(e)` to get a sense of what is being passed. – Dustin Michels Mar 02 '19 at 02:27
  • To add to that what does your Action creation script look like (the `CardService.newAction()` stuff)? Especially the `setComposeAction() ` area. – Chris Mar 02 '19 at 06:40
  • Thank you so much for looking into this, Dustin and Chris. I've updated the question with the details you asked for – ack_inc Mar 04 '19 at 09:56
  • After doing a ton of searching, I came across this open issue [https://issuetracker.google.com/issues/119968497] in the gmail addons issue tracker that makes me think updating draft emails' recipient and subject fields from an addon that extends the compose UI is not yet supported – ack_inc Mar 04 '19 at 09:58

1 Answers1

0

As this open issue indicates, filling the recipient and subject fields of an email draft from an addon that extends the compose UI is not currently supported.

ack_inc
  • 1,015
  • 7
  • 13