首页 > 解决方案 > 无效的主题名称不匹配

问题描述

我整天都在尝试解决这个问题,但我找不到错误在哪里。我正在制作一个程序来检测我何时在我的 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);

}

我在网络中放入的函数 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/* 

太奇怪了,因为我从不使用这个名字“sys-72285619869091378116913905”而且我没有在同一个地方看到它

我也允许 gmail-api-push@system.gserviceaccount.com 在此处输入图像描述

我认为问题出在项目的 id 上,所以我查看了该部分,这就是我发现的: 在此处输入图像描述

我也尝试将 topicName 更改为:

projects/sys-72285619869091378116913905/topics/mailsuc

但出现这个其他错误:

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

但是我确信我已经创建了该资源,因为我在这里做了:

在此处输入图像描述

最后一件事是我用这个 youtube 视频来指导我youtube.com/watch?v=wjHp9_NAEJo

标签: google-apps-scriptgoogle-cloud-platformgmail-apiwatchgoogle-cloud-pubsub

解决方案


watch 函数文档对这个项目有这样的说法:

请注意,“my-project-identifier”部分必须与您的 Google 开发者项目 ID(执行此监视请求的 ID)完全匹配。

sys-72285619869091378116913905大概是运行 Apps 脚本的项目的项目 ID。这是此处所述的“默认云项目” 。这显然不匹配proyecgmailyou

要解决此问题,您可以通过以下步骤将您的 Apps 脚本的云项目更改为“标准”云平台项目:

  1. 打开脚本的脚本编辑器
  2. 转到“资源 > 云平台项目...”
  3. 在此处输入您的云项目编号(即 729073306366)

授权将丢失,但此后 Apps 脚本将在您的云项目中运行,因此可以使用该项目中的主题。


推荐阅读