mrp*_*877 6 watch google-apps-script google-cloud-platform gmail-api google-cloud-pubsub
我一整天都在试图解决这个问题,但我找不到错误在哪里。我正在制作一个程序来检测我何时在我的 gmail 中收到邮件并在 Google 电子表格中写入该邮件,我正在使用 Gmail API 和 Google Apps 脚本。这是我的代码:
gs代码是:
function doPost(e) {
var message = JSON.parse(e.postData.getDataAsString()).message
var data = Utilities.newBlob(Utilies.base64Decode(message.data)).getDataAsString()[0];
var ss = SpreadsheetApp.openById('1b8s5PLItCsmk8l1q0T1KHYOzjW7iDv4sRXSFvAxVFbQ').getSheets()[0];
ss.appendRow([new Date(), message.message_id,data]);
return 200;
}
function capturemail(){
var WatchRes = Gmail.newWatchRequest();
WatchRes.labelIds = ["INBOX"];
//WatchRes.labelFilterAction = "include";
WatchRes.topicName = "projects/proyecgmailyou/topics/mailsuc";
var response = Gmail.Users.watch(WatchRes,"rjdelrio@uc.cl");
Logger.log(response);
}
Run Code Online (Sandbox Code Playgroud)
函数 doPost() 我放在一个网络中:https : //script.google.com/a/uc.cl/macros/s/AKfycby8gOrWrMDkaAlgNdXNHl2J424Hvv0yu2CKKhJQW41Ka3Xa55g/exec
然后我尝试运行功能 capturemail 但出现下一个错误:
The API call to gmail.users.watch failed with the error: Invalid topicName does not match projects/sys-72285619869091378116913905/topics/*
Run Code Online (Sandbox Code Playgroud)
很奇怪,因为我从不使用这个名字“sys-72285619869091378116913905”而且我没有在同一个地方看到它
我也允许 gmail-api-push@system.gserviceaccount.com

我认为问题出在项目的 id 上,所以我查看了该部分,这就是我发现的:

我还尝试更改 topicName 为:
projects/sys-72285619869091378116913905/topics/mailsuc
Run Code Online (Sandbox Code Playgroud)
但出现另一个错误:
The API call to gmail.users.watch failed with the error: Error sending test message to Cloud PubSub projects/sys-72285619869091378116913905/topics/mailsuc : Resource not found (resource=mailsuc).
Run Code Online (Sandbox Code Playgroud)
但是我确信我创建了该资源,因为我在这里做了:
最后一件事是我用 youtube 的这个视频来指导我youtube.com/watch?v=wjHp9_NAEJo
watch 函数文档对这个项目有这样的说法:
请注意,“my-project-identifier”部分必须与您的 Google 开发者项目 ID(执行此监视请求的 ID)完全匹配。
sys-72285619869091378116913905大概是运行 Apps 脚本的项目的项目 ID。这是此处所述的“默认云项目” 。这显然不匹配proyecgmailyou。
要解决此问题,您可以通过以下步骤将 Apps 脚本的 Cloud 项目更改为“标准”Cloud Platform 项目:
授权将丢失,但之后 Apps 脚本将在您的云项目中运行,因此可以使用该项目中的主题。