项目的简要描述:我希望通过 google 脚本在我的一个 gmail 帐户的设置中切换电子邮件转发选项。这将是我想在特定时间之间每晚调用的功能,将我的邮件从 main_email@gmail 转发到 secondary_email@gmail。
我很难找到通过谷歌脚本切换它的最简单方法。这里似乎描述了最简单的解决方案,他们使用 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。截断的服务器响应:{“错误”:{“错误”:[{“域": "global", "reason": "required", "message": "Login Required", "locationType": "header", ...(使用 muteHttpExceptions 选项检查完整响应)(第 4 行,文件“代码")
我知道这是表明我需要提供发出请求的凭据,但我不明白我会怎么做。我阅读了教程(https://developers.google.com/gmail/api/auth/about-auth)我需要使用 gmail 授权我的应用程序并获取 API 密钥,所以我去了谷歌开发者控制台创造那个。但是,在谷歌几个小时后,我不知道如何通过谷歌脚本进行身份验证或拨打电话。
这是切换 gmail 转发的最简单解决方案吗?如果是这样,我如何验证我的通话?如果没有,能够关闭/打开我的 gmail 转发的最简单解决方案是什么?