Gmail API 无法将任何邮件发布到 Pub/Sub 平台

Gmail API can't publish any message to Pub/Sub platform

我已经在 GCP 中设置了所有内容。在我手动发布消息后,我的服务器可以接收消息。 但问题是:当我向电子邮件帐户发送电子邮件时,Pub-Sub 系统和我的 webhook 服务器中没有任何通知!

下图是我手动发布消息后服务器可以收到消息的证明

并且如上图,当我调用list historyAPI时,可以查看到刚才发送的测试邮件信息

此外,我已经成功调用watch API,它返回:

{'historyId': '7688', 'expiration': '1574928158379'}

这是我的手表电话:

In [9]:     def set_watch(self, topicName):
   ...:         request = {
   ...:             'labelIds': ['INBOX', 'UNREAD', 'STARRED'],
   ...:             'labelFilterAction': 'include',
   ...:             #'labelIds': ['UNREAD'],
   ...:             'topicName': topicName
   ...:         }
   ...:         res = self.service.users().watch(
   ...:             userId=self.userId, body=request).execute()
   ...:         print(res)

事实上 set_watch 调用 returns 一个 200 和一个 HistoryId 具有有效的 expiration 让我认为手表设置正确向上,问题可能在 Pub/Sub 方面。

与测试的不同之处在于,您可以发布到主题并触发服务器响应,因为您使用的是您的帐户之一。根据文档 here,监视消息是使用 Google 管理的服务帐户发布的:

Cloud Pub/Sub requires that you grant Gmail privileges to publish notifications to your topic.

To do this, you need to grant publish privileges to gmail-api-push@system.gserviceaccount.com. You can do this using the Cloud Pub/Sub Developer Console permissions interface following the resource-level access control instructions.

链接指向 here and here。遵循最小权限原则,我建议在资源(主题)级别授予权限。

注:

面板中的设置如下所示: