【问题标题】:How to turn on email forwarding with Google Script如何使用 Google Script 开启电子邮件转发
【发布时间】:2017-12-19 21:29:47
【问题描述】:

项目的简要描述:我希望通过 google 脚本在我的一个 gmail 帐户的设置中切换电子邮件转发选项。这将是我想在特定时间之间每晚调用的功能,将我的邮件从 main_email@gmail 转发到 secondary_email@gmail。

我很难找到通过 google 脚本切换此功能的最简单方法。这里似乎描述了最简单的解决方案,他们使用 HTTP 请求。但老实说,我并不完全了解它是如何工作的,更不用说这是最简单的方法了。

https://developers.google.com/gmail/api/v1/reference/users/settings/updateAutoForwarding

我尝试在 gmail 帐户上运行以启用/禁用电子邮件转发的代码如下:

function updateForwarding() {
    var userID = "main_email@gmail.com"
    var response = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/" + userID + "/settings/autoForwarding", {
        method: 'put',
        enabled: true,
        emailAddress: "secondary_email@gmail.com",
        disposition: "leaveInInbox"
    });

    Logger.log(response.getContentText());

}

但是我得到以下错误:

请求失败 https://www.googleapis.com/gmail/v1/users/main_email@gmail.com/settings/autoForwarding 返回代码 401。截断的服务器响应:{“错误”:{“错误”:[ {“域”:“全局”,“原因”:“必需”,“消息”:“登录 必需”,“locationType”:“标题”,...(使用 muteHttpExceptions 检查完整响应的选项)(第 4 行,文件“代码”)

我知道这表明我需要提供凭据才能提出请求,但我不明白我该怎么做。我阅读了教程 (https://developers.google.com/gmail/api/auth/about-auth) 我需要使用 gmail 授权我的应用程序并获取 API 密钥,所以我去了谷歌开发者控制台来创建它。但是,在谷歌几个小时后,我不知道如何通过谷歌脚本进行身份验证或拨打电话。

这是我得到的密钥和秘密:

这是切换 gmail 转发的最简单解决方案吗?如果是,我如何验证我的呼叫?如果没有,能够关闭/打开我的 gmail 转发的最简单解决方案是什么?

【问题讨论】:

    标签: google-apps-script oauth gmail gmail-api google-apis-explorer


    【解决方案1】:

    您需要在标头信息中传递 oAuth 令牌

       function updateForwarding() {
            var userID = "main_email@gmail.com";
            var header = {
             Authorization: 'Bearer ' + ScriptApp.getOAuthToken(),
             }
            var response = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/" + userID + "/settings/autoForwarding", {
                method: 'put',
                enabled: true,
                headers: header, 
                emailAddress: "secondary_email@gmail.com",
                disposition: "leaveInInbox"
            });
    
            Logger.log(response.getContentText());
    
        }
    

    【讨论】:

    • 这不起作用,但这次我得到了一个不同的错误!错误 403,权限不足。查看其他 SO 文章似乎最常见的解决方案是将用户 ID 更改为“我”,但它仍然会导致相同的错误。知道我应该怎么做才能解决这个问题吗?
    • 嗨@Ritz,你能解释一下为什么你选择使用HTTP请求而不是Gmail API提供的方法吗?例如,为什么不使用 Gmail.Users.Settings.updateForwarding()?此外,当我有 OAuth、服务帐户和域范围的委派设置时,我无法通过使用这些方法更新域中其他用户的设置来使其工作。我收到“GoogleJsonResponseException: Not Found”错误”。它只适用于 HTTP 请求。你知道这是为什么吗?
    【解决方案2】:

    正如https://developers.google.com/gmail/api/v1/reference/users/settings/updateAutoForwarding 的授权部分所述,您需要使用具有给定范围的 OAuth 来进行调用,而不仅仅是 API 密钥。您似乎有一个客户端 ID,但您需要将其插入一个库来为您处理 OAuth 过程。然后,OAuth 流程会给您一个 Bearer 令牌以添加到您的请求中(尽管大多数 OAuth 库会为您处理这个)。

    如果您正在使用 UrlFetchApp(基于 https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app),目前推荐使用 https://github.com/googlesamples/apps-script-oauth2 来执行此操作。

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 2014-01-12
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 2012-01-22
      相关资源
      最近更新 更多