简体   繁体   English

Gmail API 是否可以使用 Gmail 附加组件的当前消息访问令牌(或等效项)?

[英]Is it at all possible to use the current message access token (or equivalent) for Gmail Add-ons with the Gmail API?

Background:背景:

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 页,因此我无法从此处找到更多信息。

Question:问题:

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)

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 by ScriptApp.getOAuthToken() . You must use the token provided in the action event object."使用ScriptApp.getOAuthToken() - 根据文档, “启用 Gmail 范围的访问令牌与ScriptApp.getOAuthToken()返回的访问令牌不同。您必须使用在操作事件 object 中提供的令牌。”
  • Providing the access token from the event object as the token in the Authorization: Bearer header also throws a 403 error.提供来自事件 object 的访问令牌作为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。

Sample script:示例脚本:

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

Note:笔记:

  • In this case, please be carefult about the scopes.在这种情况下,请注意范围。 Ref 参考

References:参考:

Added:添加:

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.readonlyscript.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参考

Answer:回答:

At the moment, this is not possible to do.目前,这是不可能的。

Issue rundown:问题概要:

  • GmailApp has no method or class to allow labels to be manipulated on individual messages, only threads. GmailApp没有方法或 class 允许在单个邮件上操作标签,只有线程。
  • The 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 令牌不同,因此不能用于验证读取当前消息的调用。
  • There is no way to pass the token via the Gmail API so that the current message can be used.没有办法通过 Gmail API 传递令牌,以便可以使用当前消息。
  • The only way to get the message is if a scope such as gmail.readonly is used.获取消息的唯一方法是使用 scope,例如gmail.readonly
  • While 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。

Further steps:进一步的步骤:

  • I have filed a feature request here to add the ability to manipulate message labels from the GmailApp class in Apps Script.我已在此处提交功能请求,以添加在 Apps 脚本中从 GmailApp class 操作邮件标签的功能。 Please star this issue to see its priority increased.请对此问题加注星标以查看其优先级增加。
    • On this note, there was a feature request for this already at this page for the same feature, but as it is over two years old without a response from Google I have filed another.在此说明中,此页面上已经有针对相同功能的功能请求,但由于它已经使用了两年多而没有 Google 的回复,我已经提交了另一个请求。
  • I have filed documentation feedback for messages.get using the Send feedback button to either have the scope gmail.addons.current.message.readonly clarified, or removed from the page.我已经使用发送反馈按钮提交了messages.get的文档反馈,以澄清scope gmail.addons.current.message.readonly或从页面中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM