通过 Google Cloud PubSub 继续获得 'Error sending test message to Cloud PubSub...'
Keep getting 'Error sending test message to Cloud PubSub...' with Google Cloud PubSub
我正在尝试设置 Google 将 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);
});
弄清楚为什么我收到错误,这是因为我没有在第 4 步中将数据作为 JSON 发送。
第 4 步中的正确代码是(注意我使用的是 json: 而不是 body:第 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 将 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);
});
弄清楚为什么我收到错误,这是因为我没有在第 4 步中将数据作为 JSON 发送。
第 4 步中的正确代码是(注意我使用的是 json: 而不是 body:第 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);
});