向Google PubSub

时间:2017-05-30 10:11:26

标签: python google-cloud-platform google-cloud-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

中描述的步骤操作

1 个答案:

答案 0 :(得分:0)

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