StatusCode.PERMISSION_DENIED 向 Google PubSub 发布消息时出错

StatusCode.PERMISSION_DENIED error while publishing message to Google PubSub

我正尝试在 Python

中将消息发布到 Google PubSub

这是我试过的代码

from google.cloud import pubsub
ps = pubsub.Client()
topic = ps.topic("topic_name")
topic.publish("Message to topic")

我收到以下错误

  File "/usr/local/lib/python2.7/dist-packages/google/cloud/iterator.py", line 218, in _items_iter
    for page in self._page_iter(increment=False):
  File "/usr/local/lib/python2.7/dist-packages/google/cloud/iterator.py", line 247, in _page_iter
    page = self._next_page()
  File "/usr/local/lib/python2.7/dist-packages/google/cloud/iterator.py", line 445, in _next_page
    items = six.next(self._gax_page_iter)
  File "/usr/local/lib/python2.7/dist-packages/google/gax/__init__.py", line 455, in next
    return self.__next__()
  File "/usr/local/lib/python2.7/dist-packages/google/gax/__init__.py", line 465, in __next__
    response = self._func(self._request, **self._kwargs)
  File "/usr/local/lib/python2.7/dist-packages/google/gax/api_callable.py", line 376, in inner
    return a_func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/google/gax/retry.py", line 127, in inner
    ' classified as transient', exception)
google.gax.errors.RetryError: GaxError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.PERMISSION_DENIED, User not authorized to perform this action.)>)

我已经下载了service-account.json并且service-account.json的路径设置为GOOGLE_APPLICATION_CREDENTIALS 我也试过安装 gcloud 并执行 gcloud auth application-default login

请注意,我可以使用 gcloud 命令在 java

中发布消息
$ gcloud beta pubsub topics publish sample "hello"
messageIds: '127284267552464'

Java代码 TopicName topicName = TopicName.create(SRC_PROJECT, SRC_TOPIC);

    Publisher publisher = Publisher.defaultBuilder(topicName).build();
    ByteString data1 = ByteString.copyFromUtf8("hello");
    PubsubMessage pubsubMessage1 = PubsubMessage.newBuilder().setData(data1).build();
    publisher.publish(pubsubMessage1);

python 代码中缺少什么?

我按照 here

中描述的步骤操作

这是我的设置问题。我的服务-account.json 没有足够的权限将消息发布到 PubSub