通过Google Cloud PubSub不断收到“正在将测试消息发送到Cloud PubSub ...错误" [英] Keep getting 'Error sending test message to Cloud PubSub...' with Google Cloud PubSub

查看:100
本文介绍了通过Google Cloud PubSub不断收到“正在将测试消息发送到Cloud PubSub ...错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to set up Google's push PubSub to my server to receive Gmail push notifications.

我得到以下范围:

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

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

It works to create a topic, subscribe to that topic, grant access to the Gmail API on that topic but it fails when I'm trying to watch my inbox. I have followed this guide: https://developers.google.com/gmail/api/guides/push and this is the code I'm using to do the steps above:

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:gmail-api-push@system.gserviceaccount.com"]
       }]
     }
   },
   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发送.

Figured out why I got the error and it was because I didn't send the data as JSON in step 4.

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

The correct code in step 4 is (notice that I'm using json: instead of body: on row 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);
});

这篇关于通过Google Cloud PubSub不断收到“正在将测试消息发送到Cloud PubSub ...错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆