1
votes

I am trying to publish messages to Google PubSub in Python

Here is the code that I tried

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

I am getting the below error

  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.)>)

I've download service-account.json and the path of service-account.json is set to GOOGLE_APPLICATION_CREDENTIALS I've also tried installing gcloud and executing gcloud auth application-default login

Please note that I am able to publish message using gcloud command and in java

$ gcloud beta pubsub topics publish sample "hello"
messageIds: '127284267552464'

Java code 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);

What is missing in python code?

I followed the steps described over here

1

1 Answers

1
votes

This is an issue with my setup. My service-account.json does not have enough permission to publish messages to PubSub