Home > Enterprise >  Not able to access current email details, in Gmail-Addon
Not able to access current email details, in Gmail-Addon

Time:09-25

I am creating a Gmail Addon and need to access attachments associated with an email, in the addon. However, I get the following error, when testing the addon :

Exception: Access denied: : Access token does not grant access to the requested thread: msg-f:1709866191276978905.

I have set the following scopes in oauth:

"oauthScopes": [
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/gmail.addons.current.message.readonly",
    "https://www.googleapis.com/auth/gmail.addons.current.message.action",
    "https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/userinfo.email"
  ]

And this is the code snippet:

function startApp(e) {
    return getDocsUI(e);
}

function getDocsUI(e){
  GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken);
  let currentEmail = GmailApp.getThreadById(e.messageMetadata.messageId);
  return HtmlService.createHtmlOutput().append(`<div>'Accessing docs'</div>`);
}

I have already tried other variants of this question, that were present on stack-overflow. Please do let me know what I have done wrong.

CodePudding user response:

Two things

  1. eventObject.messageMetadata.accessToken is deprecated, in the future you should use eventObject.gmail.accessToken instead. Same goes for eventObject.gmail.messageId instead of eventObject.messageMetadata.messageId.

  2. message and thread are not the same thing. For retrieving a message you should use the method GmailApp.getMessageById(eventObject.gmail.messageId) instead of getThreadById(eventObject.gmail.messageId).

  • Related