【问题标题】:Keep getting 'Error sending test message to Cloud PubSub...' with Google Cloud PubSub使用 Google Cloud PubSub 不断收到“向 Cloud PubSub 发送测试消息时出错...”
【发布时间】:2015-11-10 05:14:03
【问题描述】:

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

我得到以下范围:

它可以创建一个主题、订阅该主题、授予对该主题的 Gmail API 访问权限,但是当我尝试查看我的收件箱时它失败了。我已遵循本指南: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: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);
});

【问题讨论】:

  • “但是当我试图查看我的收件箱时它失败了”,请详细说明。您是否在 watch() 调用中收到某种错误消息/异常,还是其他原因。如果有异常/错误,请分享。
  • 您是否授予了您主题的发布权限?
  • @TakashiMatsuo 我不知道,如何检查我是否已授予发布权?
  • @FurhanShabir 抱歉,我从 API 收到的错误消息是:Error sending test message to Cloud PubSub projects/projectId/topics/mailSync : User not authorized to perform this action.。状态码 403
  • @TakashiMatsuo 如果我使用cloud.google.com/pubsub/reference/rest/v1/projects.topics/…,它会给我一个etag,当我记录对该请求的响应时,第3步返回了该标签,所以我想我已授予Gmail关于该主题的发布权限?跨度>

标签: javascript gmail-api google-cloud-pubsub


【解决方案1】:

找出我收到错误的原因,这是因为我没有在第 4 步中将数据作为 JSON 发送。

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

// 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);
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
  • 2018-02-18
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
  • 2018-06-22
相关资源
最近更新 更多