Creating draft email from gmail addon that extends

2019-07-28 23:07发布

问题:

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=[]
    }
}

回答1:

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.