从我的Gmail插件撰写电子邮件草稿

时间:2019-02-22 06:53:19

标签: google-apps-script gmail-addons

我正在使用示例here指导我编写代码,以使用gmail插件中的用户输入值编写独立的电子邮件草稿。

这是应该创建草稿的按钮小部件:

var submitButton = CardService.newTextButton()
    .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
    .setText('Create Draft')
    .setComposeAction(
        CardService.newAction().setFunctionName("createEmailDraft"),
        CardService.ComposedEmailType.STANDALONE_DRAFT
    );

这是回调:

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

我一直遇到以下错误:

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

我的范围似乎还不错(如果有的话,太宽容了):

"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"
],

对于克服此错误的任何帮助,我将不胜感激!

1 个答案:

答案 0 :(得分:0)

这个有点deeper in the documentation,但是您必须使用callback Action event中的访问令牌来授权草稿创建:

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