Background:
Google Workspace Add-ons for Gmail allow access the currently-open email in the API on contextualTrigger with the scope:
https://www.googleapis.com/auth/gmail.addons.current.message.readonly
An access token must be passed using GmailApp.setCurrentMessageAccessToken(accessToken)
in order to grant access to this currently opened email:
var accessToken = e.gmail.accessToken;
var messageId = e.gmail.messageId;
// The following function enables short-lived access to the current
// message in Gmail. Access to other Gmail messages or data isn't
// permitted.
GmailApp.setCurrentMessageAccessToken(accessToken);
var mailMessage = GmailApp.getMessageById(messageId);
From the above documentation:
setCurrentMessageAccessToken(accessToken)
Sets the current message access token that enables the script to access the current
GmailMessage
properties.Only Gmail add-on projects using Gmail current message scopes require this method.
Unfortunately, the link to the pages on access tokens and current message scopes are at current both broken and result in 404 pages, so I can't find out more information from here.
Question:
How can one achieve the same funcitonality using the Gmail API directly instead of GmailApp
?
The documentation for Gmail: users.messages.get states that https://www.googleapis.com/auth/gmail.addons.current.message.readonly
is a valid scope to call this method, however, there does not seem to be an equivalent of GmailApp.setCurrentMessageAccessToken(accessToken)
for the Gmail API.
Things that do not work:
- Using
ScriptApp.getOAuthToken()
- as per this documentation, "The access token than enables Gmail scopes is not the same as the access token returned byScriptApp.getOAuthToken()
. You must use the token provided in the action event object." - Providing the access token from the event object as the token in the
Authorization: Bearer
header also throws a 403 error.
The saught functionality would akin to:
var accessToken = e.gmail.accessToken
var messageId = e.gmail.messageId
Gmail.setCurrentMessageAccessToken(accessToken) // made up method
var mailMessage = Gmail.Users.Messages.get("me", messageId)