14

我正在学习 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)查看完整堆栈跟踪的日志。

我想这是由于一些身份验证问题?任何帮助,将不胜感激。

4

5 回答 5

15

确保您使用的客户端有权发布到 Pub/Sub。在此处查看详细信息。如果您的客户与主题属于同一项目,通常不需要额外的访问控制配置。确保您的客户端已正确验证

于 2016-06-16T14:50:19.220 回答
9

以下是来自 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。
于 2016-06-17T20:57:51.533 回答
5

鉴于订阅和主题,您可以单击“权限”->“添加主体”,使用服务帐户粘贴来自您的 json 的长电子邮件并选择中描述的权限

https://cloud.google.com/pubsub/docs/access-control

在此处输入图像描述

于 2021-09-29T13:13:22.820 回答
0

按照丹尼尔的回答,我试图给自己的Pub/Sub Editor角色Pub/Sub Publisher角色,但没有奏效,对我有用的是Pub/Sub Admin

于 2021-12-16T17:10:22.627 回答
0

最新的 python 演示有效,但 ruby​​ API 需要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 

关于 python 示例设置的注意事项:

  • 使用 python 3.10.1
  • 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)
于 2022-01-20T00:50:54.883 回答