gmail.users.watch 无法使用 DwD 服务帐户向 PubSub 发送测试消息
gmail.users.watch fails to send test message to PubSub with a DwD service account
我正在尝试设置 gmail.users.watch 但出现 403 错误:
Error sending test message to Cloud PubSub projects/project-id/topics/topic-id : User not authorized to perform this action.
身份验证正在使用 GOOGLE_APPLICATION_CREDENTIALS
方法和下载的凭证 json 文件。
以下代码工作正常,支持我的假设,即身份验证通常有效:
const pubsub = PubSub();
const topic = pubsub.topic('topic-id');
const subscription = pubsub.subscription('subscription-id');
topic.exists()
.then(data => {
console.log(data);
return subscription.exists();
})
.then(data => {
console.log(data);
return subscription.pull()
})
.then(data => {
data[1].receivedMessages.forEach(d => console.log(d));
return topic.publish('Hello, world!');
})
.then(data => {
console.log(data)
})
.catch(err => console.log(err));
该代码没有错误。但是,以下代码会抛出上述 403 错误:
const authParams = {
subject: userId,
scopes: [
'https://mail.google.com/',
'https://www.googleapis.com/auth/pubsub'
]
};
gauth.getAuth(authParams)
.then(authClient => {
const params = {
auth: authClient,
userId: 'me',
resource: {
topicName: <topic-id>
}
};
return new Promise((resolve, reject) => {
gmail.users.watch(params, (err, response) => {
if (err) {
console.log(err);
reject(err);
return;
}
resolve(response);
});
});
})
.then(response => {
console.log(response);
});
gauth.getAuth
是 getApplicationDefault
Google Auth Library for Node.js.
的简单包装
G Suite 域安全客户端访问根据所需范围配置了服务帐户的客户端 ID:
https://www.googleapis.com/auth/pubsub
、https://mail.google.com/
由于原生云 pub/sub 工作正常,我认为服务帐户在控制台上配置了所有正确的权限,所以我对 gmail 调用失败的原因有点不知所措。
更新:
服务帐户在 Google Cloud Console 上具有以下权限:
项目:(是否需要更多的 gmail 内容?)
- 服务帐户参与者
- PubSub 管理员
主题:
- 所有者
订阅:
- 所有者
进行 gmail 调用时,服务帐户将委托给 'subject'。我已将该主题 userId 的权限添加到 Google Cloud Console:
项目:
- 所有者
主题:
- 所有者
订阅:
- 所有者
您需要添加权限才能将消息发布到您的订阅。
转到 Google 云控制台,select 订阅,然后是权限。
在“添加成员”部分,键入 allAuthenticatedUsers、select Pub/Sub 发布者角色 ,然后单击 添加
有帮助吗?
更新(2017/05/09):
我正在编辑我的答案,以便更准确地说明需要的内容。
根据 Gmail API documentation(请参阅 授予您主题的发布权 部分:
- 需要在主题级别设置权限
- 会员是:serviceAccount:gmail-api-push@system.gserviceaccount.com
- 角色是:Pub/Sub发布者
我正在尝试设置 gmail.users.watch 但出现 403 错误:
Error sending test message to Cloud PubSub projects/project-id/topics/topic-id : User not authorized to perform this action.
身份验证正在使用 GOOGLE_APPLICATION_CREDENTIALS
方法和下载的凭证 json 文件。
以下代码工作正常,支持我的假设,即身份验证通常有效:
const pubsub = PubSub();
const topic = pubsub.topic('topic-id');
const subscription = pubsub.subscription('subscription-id');
topic.exists()
.then(data => {
console.log(data);
return subscription.exists();
})
.then(data => {
console.log(data);
return subscription.pull()
})
.then(data => {
data[1].receivedMessages.forEach(d => console.log(d));
return topic.publish('Hello, world!');
})
.then(data => {
console.log(data)
})
.catch(err => console.log(err));
该代码没有错误。但是,以下代码会抛出上述 403 错误:
const authParams = {
subject: userId,
scopes: [
'https://mail.google.com/',
'https://www.googleapis.com/auth/pubsub'
]
};
gauth.getAuth(authParams)
.then(authClient => {
const params = {
auth: authClient,
userId: 'me',
resource: {
topicName: <topic-id>
}
};
return new Promise((resolve, reject) => {
gmail.users.watch(params, (err, response) => {
if (err) {
console.log(err);
reject(err);
return;
}
resolve(response);
});
});
})
.then(response => {
console.log(response);
});
gauth.getAuth
是 getApplicationDefault
Google Auth Library for Node.js.
G Suite 域安全客户端访问根据所需范围配置了服务帐户的客户端 ID:
https://www.googleapis.com/auth/pubsub
、https://mail.google.com/
由于原生云 pub/sub 工作正常,我认为服务帐户在控制台上配置了所有正确的权限,所以我对 gmail 调用失败的原因有点不知所措。
更新: 服务帐户在 Google Cloud Console 上具有以下权限:
项目:(是否需要更多的 gmail 内容?)
- 服务帐户参与者
- PubSub 管理员
主题:
- 所有者
订阅:
- 所有者
进行 gmail 调用时,服务帐户将委托给 'subject'。我已将该主题 userId 的权限添加到 Google Cloud Console:
项目:
- 所有者
主题:
- 所有者
订阅:
- 所有者
您需要添加权限才能将消息发布到您的订阅。
转到 Google 云控制台,select 订阅,然后是权限。 在“添加成员”部分,键入 allAuthenticatedUsers、select Pub/Sub 发布者角色 ,然后单击 添加
有帮助吗?
更新(2017/05/09):
我正在编辑我的答案,以便更准确地说明需要的内容。
根据 Gmail API documentation(请参阅 授予您主题的发布权 部分:
- 需要在主题级别设置权限
- 会员是:serviceAccount:gmail-api-push@system.gserviceaccount.com
- 角色是:Pub/Sub发布者