我正在使用 node.js 创建一个应用程序,该应用程序每次收到电子邮件时都会从 Gmail 获取 PUSH,将其与 CRM 中的第三方数据库进行检查,并在 CRM 中创建一个新字段(如果电子邮件包含在其中) . 我在使用 Google 的新 Cloud Pub/Sub 时遇到了问题,这似乎是无需持续轮询即可从 Gmail 获取推送的唯一方法。
我已经阅读了这里的说明:https ://cloud.google.com/pubsub/prereqs但我不明白这应该如何从我桌面上的应用程序中运行。似乎 pub/sub 可以连接到经过验证的域,但我无法让它直接连接到我计算机上的 .js 脚本。我已将 api 密钥保存在 json 文件中并使用以下内容:
var gcloud = require('gcloud');
var pubsub;
// From Google Compute Engine:
pubsub = gcloud.pubsub({
projectId: 'my-project',
});
// Or from elsewhere:
pubsub = gcloud.pubsub({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
// Create a new topic.
pubsub.createTopic('my-new-topic', function(err, topic) {});
// Reference an existing topic.
var topic = pubsub.topic('my-existing-topic');
// Publish a message to the topic.
topic.publish('New message!', function(err) {});
// Subscribe to the topic.
topic.subscribe('new-subscription', function(err, subscription) {
// Register listeners to start pulling for messages.
function onError(err) {}
function onMessage(message) {}
subscription.on('error', onError);
subscription.on('message', onMessage);
// Remove listeners to stop pulling for messages.
subscription.removeListener('message', onMessage);
subscription.removeListener('error', onError);
});
但是,我收到错误,好像它没有连接到服务器并且在 API 列表中我只看到错误,没有实际成功。我显然做错了什么,知道可能是什么吗?
先感谢您!