我正在学习 Google Cloud Pub/Sub 并遵循这个官方文档:Writing and Responding to Pub/Sub Messages - Python
发生内部错误:403 用户无权执行此操作。(发布https://pubsub.googleapis.com/v1/projects/your-project-id/topics/your-topic:publish)查看完整堆栈跟踪的日志。
我想这是由于一些身份验证问题?任何帮助,将不胜感激。
我正在学习 Google Cloud Pub/Sub 并遵循这个官方文档:Writing and Responding to Pub/Sub Messages - Python
发生内部错误:403 用户无权执行此操作。(发布https://pubsub.googleapis.com/v1/projects/your-project-id/topics/your-topic:publish)查看完整堆栈跟踪的日志。
我想这是由于一些身份验证问题?任何帮助,将不胜感激。
以下是来自 google 文档Troubleshooting 403 (Forbidden) error的一些注释:
如果您收到此错误,请执行以下操作:
- 确保您已在 Cloud Platform Console 中启用 Google Cloud Pub/Sub API。
- 确保发出请求的委托人对相关的 Google Cloud Pub/Sub 资源具有所需的权限,
尤其是在您使用 Google Cloud Pub/Sub 进行跨项目
通信时。- 如果您使用的是 Dataflow,请确保 @cloudservices.gserviceaccount.com 和 Compute Engine
服务帐户-compute@developer.gserviceaccount.com 对相关的 Google Cloud Pub/Sub 资源
具有所需的权限。
有关详细信息,请参阅 Google Cloud Dataflow 安全性和权限。- 如果您使用的是 App Engine,请检查您项目的权限页面以查看 App Engine 服务帐户是否列为编辑者。如果不是,请将您的 App Engine 服务帐户添加为编辑器。通常,App Engine 服务帐户的格式为
@appspot.gserviceaccount.com。
鉴于订阅和主题,您可以单击“权限”->“添加主体”,使用服务帐户粘贴来自您的 json 的长电子邮件并选择中描述的权限
按照丹尼尔的回答,我试图给自己的Pub/Sub Editor
角色Pub/Sub Publisher
角色,但没有奏效,对我有用的是Pub/Sub Admin
viewer
许可Google 视频系列#pubsubmadeeasy Cloud Pub/Sub in Action的第 3 集中显示的 python 演示使用视频指示您添加的权限与更新的代码(请参阅下面的注释)运行良好,特别是
Pub/Sub Publisher
Pub/Sub Subscriber
但是切换到ruby API需要将查看者权限添加到演示中创建的服务帐户。注意:使用 ruby3.1.0
和gem 'google-cloud-pubsub', '~> 2.9', '>= 2.9.1'
.
Pub/Sub Viewer
https://github.com/googleapis/python-pubsub.git
具体
示例代码
https://github.com/googleapis/python-pubsub/tree/main/samples/snippets/quickstart
virtualenv venv && source venv/bin/activate
pip install --upgrade google-cloud-pubsub
export GOOGLE_APPLICATION_CREDENTIALS=downloaded_key.json
export PROJECT=`gcloud config get-value project`
/home/dever/.gem/ruby/3.1.0/gems/google-cloud-pubsub-v1-0.6.2/lib/google/cloud/pubsub/v1/subscriber/client.rb:499:
in `rescue in get_subscription':
7:User not authorized to perform this action.. debug_error_string:
{"created":"@1642638738.357361477",
"description":"Error received from peer ipv6:[2607:f8b0:4006:80a::200a]:443",
"file":"src/core/lib/surface/call.cc","file_line":1063,
"grpc_message":"User not authorized to perform this action.","grpc_status":7}
(Google::Cloud::PermissionDeniedError)
/home/dever/.gem/ruby/3.1.0/gems/google-cloud-pubsub-v1-0.6.2/lib/google/cloud/pubsub/v1/publisher/client.rb:574:
in `rescue in get_topic':
7:User not authorized to perform this action..
debug_error_string:{"created":"@1642638676.763569110",
"description":"Error received from peer ipv6:[2607:f8b0:4006:80a::200a]:443",
"file":"src/core/lib/surface/call.cc","file_line":1063,
"grpc_message":"User not authorized to perform this action.","grpc_status":7}
(Google::Cloud::PermissionDeniedError)