Javascript 保持';向Cloud PubSub发送测试消息时出错…';使用谷歌云PubSub

Javascript 保持';向Cloud PubSub发送测试消息时出错…';使用谷歌云PubSub,javascript,gmail-api,google-cloud-pubsub,Javascript,Gmail Api,Google Cloud Pubsub,我正在尝试将Google的push PubSub设置到我的服务器,以接收Gmail推送通知 我得到了以下范围: 它可以创建一个主题,订阅该主题,允许访问该主题的Gmail API,但当我试图查看收件箱时,它失败了。我遵循了以下指南:这是我用于执行上述步骤的代码: var rp = require('request-promise'); // Step 1. Create a topic rp({ url: 'https://pubsub.googleapis.com/v1/

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

我得到了以下范围:


它可以创建一个主题,订阅该主题,允许访问该主题的Gmail API,但当我试图查看收件箱时,它失败了。我遵循了以下指南:这是我用于执行上述步骤的代码:

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发送

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

//第4步。查看我的收件箱
反相({
url:“https://www.googleapis.com/gmail/v1/users/me/watch",
方法:“张贴”,
标题:{
授权:“承载访问令牌”
},

json:{“但当我试图监视我的收件箱时它失败了”,请详细说明。您是否在watch()上收到某种错误消息/异常调用或是其他内容。请共享异常/错误(如果有)。您是否已授予主题的发布权限?@TakashiMatsuo我不知道,如何检查我是否已授予发布权限?@FurhanShabir对此表示抱歉,我从API收到的错误消息是:
将测试消息发送到Cloud PubSub Project/projectId/topics/mail时出错同步:未授权用户执行此操作。
.Statuscode403@TakashiMatsuo如果我使用它,它会给我一个etag,当我记录对该请求的响应时,步骤3返回了该etag,所以我想我已经授予Gmail该主题的发布权限了?您在开始时提到的范围是应用程序级别的,这意味着在谷歌控制台为该应用程序或其用户访问令牌的源代码启用它们?
// 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);
});