使用 Google Cloud PubSub 时不断收到“向 Cloud PubSub 发送测试消息时出错...”

2024-03-30

我正在尝试将 Google 的推送 PubSub 设置到我的服务器以接收 Gmail 推送通知。

我得到以下范围:

  • https://mail.google.com/ https://mail.google.com/
  • https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/cloud-platform
  • https://www.googleapis.com/auth/pubsub https://www.googleapis.com/auth/pubsub
  • https://www.googleapis.com/auth/gmail.modify https://www.googleapis.com/auth/gmail.modify
  • https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/gmail.readonly

它可以创建一个主题、订阅该主题、授予对该主题的 Gmail API 的访问权限,但当我尝试查看我的收件箱时,它会失败。我已经遵循了这个指南:https://developers.google.com/gmail/api/guides/push https://developers.google.com/gmail/api/guides/push这是我用来执行上述步骤的代码:

var rp = require('request-promise');

// Step 1. Create a topic
rp({
   url: 'https://pubsub.googleapis.com/v1/projects/projectId/topics/mailSync',
   method: 'PUT',
   headers: {
     Authorization: 'Bearer accessToken'
   }
 }).then(function(response) {
   console.log(response);
   res.send(response);
 })
 .catch(function(error) {
   console.log(error.message);
   res.send(error.message);
 });

// Step 2. Create a subscription:
rp({
   url: 'https://pubsub.googleapis.com/v1/projects/projectId/subscriptions/mailSync',
   method: 'PUT',
   headers: {
     Authorization: 'Bearer accessToken'
   },
   json: {
     topic: 'projects/projectId/topics/mailSync',
     pushConfig: {
       pushEndpoint: 'https://developers.example.com/mailSyncHandler'
     }
   }
 }).then(function(response) {
   console.log(response);
   res.send(response);
 })
 .catch(function(err) {
   console.error(err);
   res.status(err.statusCode).send(err.error.error.message);
 });

// Step 3. Grant the Gmail API publish rights on our topic
rp({
   url: "https://pubsub.googleapis.com/v1beta2/projects/projectId/topics/mailSync:setIamPolicy",
   method: 'POST',
   headers: {
     Authorization: 'Bearer accessToken'
   },
   data: {
     policy: {
       bindings: [{
         role: "roles/pubsub.publisher",
         members: ["serviceAccount:[email protected] /cdn-cgi/l/email-protection"]
       }]
     }
   },
   json: true
 }).then(function(response) {
   console.log(response);
   res.send(response);
 })
 .catch(function(error) {
   console.log(error.message);
   res.send(error.message);
 });

// Step 4. Watch my Inbox
rp({
  url: "https://www.googleapis.com/gmail/v1/users/me/watch",
  method: "POST",
  headers: {
    Authorization: 'Bearer accessToken'
  },
  json: {
    topicName: "projects/projectId/topics/mailSync",
    labelIds: ["INBOX"]
  }
}).then(function(response) {
  console.log(response);
  res.send(response);
})
.catch(function(error) {
  console.error(error);
  res.send(error.message);
});

弄清楚为什么我收到错误,这是因为我没有在步骤 4 中以 JSON 形式发送数据。

步骤 4 中的正确代码是(请注意,我正在使用json:代替body:第 8 行):

// Step 4. Watch my Inbox
rp({
  url: "https://www.googleapis.com/gmail/v1/users/me/watch",
  method: "POST",
  headers: {
    Authorization: 'Bearer accessToken'
  },
  json: {     <-----
    topicName: "projects/projectId/topics/mailSync",
    labelIds: ["INBOX"]
  }
}).then(function(response) {
  console.log(response);
  res.send(response);
})
.catch(function(error) {
  console.error(error);
  res.send(error.message);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Google Cloud PubSub 时不断收到“向 Cloud PubSub 发送测试消息时出错...” 的相关文章

随机推荐

  • 无法在 websphere 8.5 上启动应用程序,但在版本 7 上运行

    我遇到了一个特殊的问题 我有一个包含 ejb jar 的 Ear 应用程序在 websphere 7 上独立运行 我下载了 8 5 试用版 创建了一个垂直集群并在其上安装了应用程序 但该应用程序在 8 5 版本上尚未启动 每次我尝试启动它时
  • 如何删除头部?

    我错误地推送了一些文件 它在主存储库中显示了不同的头 我怎样才能删除那个头 您可以通过编辑您的文件来启用 mq 扩展 hgrc文件 确保存在以下行 extensions mq 之后 您可以 剥离 特定修订版 将其删除 这样您就只有一个头 h
  • 测试依赖于 NUnit 的常用功能

    我有一些初始化代码来使用我的 API 初始化可能会失败 我想在 NUnit 测试中测试它 初始化之后就可以使用API 了 我也在测试 API 但我所有的测试方法都将使用相同的 通用的初始化代码 我理想的情况是这种行为 运行初始化测试 如果
  • Java 8 流 - 超时?

    我想循环一个巨大的数组并执行一组需要很长时间的复杂指令 但是 如果超过 30 秒 我希望它放弃 ex final long start System currentTimeMillis myDataStructure stream whil
  • Swift 数字和 CGFloat(CGPoint、CGRect 等)

    我发现 Swift 数字特别笨拙 就像现实生活中经常发生的那样 我必须与 Cocoa Touch 就 CGRect 和 CGPoint 进行交流 例如 因为我们正在谈论某事frame or bounds CGFloat 与 Double 考
  • 尝试在 Maven 中构建具有嵌入式依赖项的 OSGi 包。似乎无法从 BND 类路径中排除传递依赖项

    基本上 我的 Web 服务必须可部署为单个 OSGi jar 包 所以 该包必须包含所有编译和运行时 Maven 依赖项 它还必须包含依赖于这些依赖项的所有非可选依赖项 即传递依赖项 我正在尝试使用 maven bundle plugin
  • 使用 java.util.TimeZone 查找 DST 转换时间戳

    是否可以使用 Java Calendar Date TimeZone API 获取上一个和下一个 DST 转换时间戳 With Joda Time我可以写 DateMidnight today new DateMidnight 2009 2
  • 无法在浏览器中显示希腊字母

    我正在使用 html 和 css 开发一个网站 但我看不到希腊字母 相反 我只看到符号 我的 html 文件中有以下行 我也尝试过
  • Backbone.js 控制器中的默认路由?

    我想为我的backbone js 控制器设置默认路由 目前我是这样做的 class DealSearchController extends Backbone Controller routes list showListView phot
  • 将 HSB/HSV 颜色转换为 HSL

    如何将 HSB 颜色转换为 HSL Photoshop 在其颜色选择器中显示 HSB 颜色 HSB 颜色不能在 CSS 中使用 但 HSL 可以 我尝试了这个JS function hsb2hsl h s b return h h s s
  • 如何创建 WPF 圆角容器?

    我们正在创建一个 XBAP 应用程序 我们需要在单个页面的各个位置具有圆角 并且我们希望有一个 WPF 圆角容器来在其中放置一堆其他元素 有人对我们如何最好地实现这一目标有一些建议或示例代码吗 是使用样式还是创建自定义控件 您不需要自定义控
  • 在 Groovy 中,为什么扩展 Comparable 的接口的“==”行为会发生变化?

    我正在尝试在 Groovy 中开发一个项目 我发现我的一些测试以一种奇怪的方式失败 我有一个界面Version extends Comparable
  • div 不会以 margin: 0 auto 居中;

    我的问题只是将 div 居中 目前 我只有一个简单的 html 文件 我不知道为什么margin 0 auto不工作 这是我的 html 的布局
  • 什么是最简单的 Tomcat/Apache 连接器 (Windows)?

    我在 Windows XP 机器上运行 apache 2 2 和 tomcat 5 5 哪个 tomcat apache 连接器最容易设置并且有详细的文档记录 mod proxy ajp http httpd apache org docs
  • 如何正确使用SyncManager.Lock或Event?

    我使用时遇到问题SyncManager Lock正确 我读了官方文档 https docs python org 3 library multiprocessing html multiprocessing managers SyncMan
  • 如何集中primefaces菜单栏?

    我需要集中 primefaces 菜单栏 我试过这个
  • 使用 Apache Ant 清理陈旧的 .class 文件

    怎样清理陈旧的东西 class文件出自 workdir 给定一组现有的 java文件在 srcdir 我所说的陈旧是指 class从现在开始生成的文件被删除 java文件 我尝试过使用 Ant 映射器和文件集等来想出一些东西 但失败了 删除
  • SlugRelatedField 查询集

    我正在努力找出 SlugRelatedField 的查询集 我的数据是这样的 我有一堆Object属于 a 的实例Project 一个项目有一个独特的 顶 Object Object仅当它们位于不同的下面时才可以具有相同的名称Project
  • 在 C# 中复制带有身份验证的文件

    我正在尝试将文件从本地驱动器复制到服务器上的文件夹之一 服务器上文件夹的名称是 DBFiles 除了用户名 user 和密码 password1 之外 没有人可以访问此内容 在复制文件之前 如果目录不存在 它也会创建该目录 有人可以帮助在创
  • 使用 Google Cloud PubSub 时不断收到“向 Cloud PubSub 发送测试消息时出错...”

    我正在尝试将 Google 的推送 PubSub 设置到我的服务器以接收 Gmail 推送通知 我得到以下范围 https mail google com https mail google com https www googleapis