从扩展 compose UI 的 gmail 插件创建电子邮件草稿

Creating draft email from gmail addon that extends compose UI

我 运行 我的 gmail 插件具有以下范围:

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

我有一个按钮和相应的撰写操作事件,它应该从某些输入字段中读取收件人、主题和正文信息,并将这些信息插入撰写 UI。

撰写操作回调如下所示:

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();
}

单击按钮时,插件崩溃并显示以下错误消息:TypeError: Cannot read property "accessToken" from undefined.

e.messageMetadata 似乎未定义。我应该在其他地方寻找访问令牌吗?

只需删除违规行

GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken);

不起作用。加载项崩溃并出现不同的错误

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

评论后更新:

这是触发撰写操作的按钮小部件的代码:

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

这是记录的 'e' 对象:

{
    formInput={body=Hello},
    parameters={},
    draftMetadata={
        toRecipients=[abcd@example.com],
        bccRecipients=[],
        ccRecipients=[]
    }
}

如此 open issue 所示,当前不支持从扩展撰写 UI 的插件中填写电子邮件草稿的收件人和主题字段。