带有服务帐户的Google Cloud Pub Sub

时间:2018-07-06 17:54:41

标签: python google-cloud-pubsub google-api-python-client

大家好,我在pub sub上遇到问题,这使我发疯。基本上,我有一个使用admin priivs的pubsub服务帐户,但是无法正常工作,并且出现以下错误:

  

ERROR:root:AuthMetadataPluginCallback“”引发了异常!   追溯(最近一次通话):     调用中的文件“ /usr/local/lib/python2.7/dist-packages/grpc/_plugin_wrapping.py”,第77行       callback_state,callback))     调用中的文件“ /usr/local/lib/python2.7/dist-packages/google/auth/transport/grpc.py”,第77行       回调(self._get_authorization_headers(context),无)     _get_authorization_headers中的文件“ /usr/local/lib/python2.7/dist-packages/google/auth/transport/grpc.py”,第61行       self._credentials.before_request(   AttributeError:“ str”对象没有属性“ before_request”

代码超级简单

 from google.cloud import pubsub

 credentials = '/home/airflow/Desktop/test/config/test.json'

 publisher = pubsub.PublisherClient(credentials=credentials)
 topic_path = publisher.topic_path("test-proj", "test")

 for n in range(1, 2):
  data = u'Message number {}'.format(n)
  # Data must be a bytestring
  data = data.encode('utf-8')
  test = publisher.publish(topic_path, data=data).result()
  print(test, "s")

我非常感谢艾米的帮助,因为该错误消息对我而言意义不大。谢谢

1 个答案:

答案 0 :(得分:2)

PublisherClient的凭据参数不是字符串。它是google.auth.credentials.Credentials对象。 google-auth-guide指示如何创建它:

from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    '/home/airflow/Desktop/test/config/test.json')