[英]Is it at all possible to use the current message access token (or equivalent) for Gmail Add-ons with the Gmail API?
Google Workspace Add-ons for Gmail allow access the currently-open email in the API on contextualTrigger with the scope:适用于 Gmail 的 Google Workspace 加载项允许使用 Z31A11818181822D 访问 API 中当前打开的 email
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:必须使用
GmailApp.setCurrentMessageAccessToken(accessToken)
传递访问令牌,以便授予对当前打开的 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.设置使脚本能够访问当前
GmailMessage
属性的当前邮件访问令牌。Only Gmail add-on projects using Gmail current message scopes require this method.
只有使用 Gmail 当前消息范围的 Gmail 附加项目需要此方法。
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.不幸的是,访问令牌和当前消息范围页面的链接目前都已损坏并导致 404 页,因此我无法从此处找到更多信息。
How can one achieve the same funcitonality using the Gmail API directly instead of GmailApp
?如何直接使用 Gmail API 而不是
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. Gmail 的文档:users.messages.get指出
https://www.googleapis.com/auth/gmail.addons.current.message.readonly
是一个有效的 Z31A1FD140BE4BEF2D11E121EC9相当于 Gmail API 的GmailApp.setCurrentMessageAccessToken(accessToken)
。
ScriptApp.getOAuthToken()
- as per this documentation, "The access token than enables Gmail scopes is not the same as the access token returned by ScriptApp.getOAuthToken()
. You must use the token provided in the action event object."ScriptApp.getOAuthToken()
- 根据本文档, “启用 Gmail 范围的访问令牌与ScriptApp.getOAuthToken()
返回的访问令牌不同。您必须使用在操作事件 object 中提供的令牌。”Authorization: Bearer
header also throws a 403 error.Authorization: Bearer
header 中的令牌也会引发 403 错误。 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)
I think that in that case, in order to include the access token, how about directly requesting to the endpoint of the Gmail API using UrlFetchApp?我认为在这种情况下,为了包含访问令牌,如何使用 UrlFetchApp 直接向 Gmail API 的端点请求? When your script is converted, it becomes as follows.
当您的脚本被转换时,它变成如下。 In this case, the scope can be selected from Authorization Scopes .
在这种情况下,可以从Authorization Scopes中选择 scope。
var accessToken = "###";
var messageId = "###";
var userId = "me";
var url = `https://gmail.googleapis.com/gmail/v1/users/${userId}/messages/${messageId}`;
var res = UrlFetchApp.fetch(url, {headers: {authorization: `Bearer ${accessToken}`}});
console.log(res.getContentText())
Your goal is as follows.您的目标如下。
You want to use the method of "users.messages.get" of Gmail API using the scope of
gmail.addons.current.message.readonly
.You want to use the method of "users.messages.get" of Gmail API using the scope of
gmail.addons.current.message.readonly
.
From our discussions in the comment, I summarized the current situation as follows.根据我们在评论中的讨论,我将目前的情况总结如下。
When I checked the including scopes from the access token retrieved by ScriptApp.getOAuthToken()
for the GAS project which set the manifest file, I could confirm that the scopes of gmail.addons.current.message.readonly
and script.external_request
.当我从设置清单文件的 GAS 项目的
ScriptApp.getOAuthToken()
检索的访问令牌中检查包含范围时,我可以确认gmail.addons.current.message.readonly
和script.external_request
的范围。
When I saw the official document , in order to use the method of "users.messages.get", gmail.addons.current.message.readonly
is shown as Requires one of the following OAuth scopes:
.当我看到官方文档时,为了使用“users.messages.get”的方法,
gmail.addons.current.message.readonly
显示为Requires one of the following OAuth scopes:
。
But, when the scope of gmail.addons.current.message.readonly
is used for the method of "users.messages.get", such error like Missing access token for authorization.
但是,当 gmail.addons.current.message.readonly 的
gmail.addons.current.message.readonly
用于“users.messages.get”方法时,会出现Missing access token for authorization.
occurs.发生。
When the scope of gmail.readonly
is included, the error could be removed.当包含 gmail.readonly 的
gmail.readonly
时,可以消除错误。
From above situation, how about reporting this to the Google issue tracker?根据上述情况,如何将其报告给 Google 问题跟踪器? Ref
参考
At the moment, this is not possible to do.目前,这是不可能的。
GmailApp
has no method or class to allow labels to be manipulated on individual messages, only threads. GmailApp
没有方法或 class 允许在单个邮件上操作标签,只有线程。accessToken
provided in the contextual trigger appears to be distinct to the OAuth token and so can not be used to authenticate the call to read the current message.accessToken
似乎与 OAuth 令牌不同,因此不能用于验证读取当前消息的调用。gmail.readonly
is used.gmail.readonly
。https://www.googleapis.com/auth/gmail.addons.current.message.readonly
is listed as a valid scope for messages.get
, there does not appear to be any way to actually use the scope when connecting to the Gmail API.https://www.googleapis.com/auth/gmail.addons.current.message.readonly
被列为messages.get
的有效 scope ,但在连接到 140BE1E4BEF8A58Z 时似乎没有任何方法可以实际使用 Z31A18FD140BE1E4BEF251D Gmail API。gmail.addons.current.message.readonly
clarified, or removed from the page.gmail.addons.current.message.readonly
或从页面中删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.