gpt4 book ai didi

javascript - gmail.user.watch 无法使用 DwD 服务帐户向 PubSub 发送测试消息

转载 作者:行者123 更新时间:2023-12-03 04:30:10 24 4
gpt4 key购买 nike

我正在尝试设置 gmail.users.watch 但收到 403 错误:

向 Cloud PubSub 项目/project-id/topics/topic-id 发送测试消息时出错:用户无权执行此操作。

身份验证正在使用 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.getAuthgetApplicationDefault Google Auth Library for Node.js 的简单包装器。 .

G Suite 域安全客户端访问是根据所需范围使用服务帐户的客户端 ID 进行配置的:https://www.googleapis.com/auth/pubsubhttps://mail.google.com/

随着 native 云发布/订阅内容的工作,我认为服务帐户具有在控制台上配置的所有正确权限,因此我对 gmail 调用失败的原因感到有点困惑。

更新:服务帐号在 Google Cloud Console 上拥有以下权限:

项目:(gmail 的东西还需要更多吗?)

  • 服务帐户参与者
  • PubSub 管理员

主题:

  • 所有者

订阅:

  • 所有者

进行 Gmail 调用时,服务帐户将委托(delegate)给“主题”。我已将该主体 userId 的权限添加到 Google Cloud Console:

项目:

  • 所有者

主题:

  • 所有者

订阅:

  • 所有者

最佳答案

您需要添加权限,以便可以将消息发布到您的订阅。

转到 Google Cloud 控制台,选择订阅,然后选择权限。在“添加成员”部分中,输入allAuthenticatedUsers,选择Pub/Sub 发布者 Angular 色,然后单击添加

有帮助吗?

更新(2017/05/09):

我正在编辑我的答案,以便更准确地了解所需内容。

根据Gmail API documentation (请参阅授予您主题的发布权部分):

  • 需要在主题级别设置权限
  • 成员是:serviceAccount:gmail-api-push@system.gserviceaccount.com
  • Angular 色是:Pub/Sub 发布者

关于javascript - gmail.user.watch 无法使用 DwD 服务帐户向 PubSub 发送测试消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43525182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com