不断收到'向云PubSub发送测试消息错误…'Google Cloud PubSub
Keep getting 'Error sending test message to Cloud PubSub...' with Google Cloud PubSub
我正在尝试将Google的push PubSub设置到我的服务器以接收Gmail的推送通知。
我得到以下作用域:
- 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,这是我用来执行上述步骤的代码:
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);
});
弄清楚为什么我得到了错误,这是因为我没有发送数据作为JSON在第4步。
步骤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);
});
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- CKFinder 3为所选文件返回错误的URL
- 同位素库错误:未捕获错误无布局模式包装生产线8
- 铬:“;未捕获的语法错误:意外的标记:"
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 相位器状态未捕获参考错误
- /undefined在我的404错误日志中多次出现
- Javascript未捕获语法错误意外的标识符错误
- javascript:如何在antlr生成的Lexer中进行错误处理
- Angularjs工厂注入错误
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- 不断收到'向云PubSub发送测试消息错误…'Google Cloud PubSub