我无法使用.json文件(而是使用隐式应用程序默认值/ GOOGLE_APPLICATION_CREDENTIALS env var)创建Pub / Sub Publisher客户端。我在做错什么吗?Pub / Sub是否有些细微差别?在这种情况下,为什么...?
对于大多数BigQuery,大多数Google客户库都允许这样做
client = bigquery.Client.from_service_account_json(json_credentials_path = ‘PATH/service_account.json')
浏览PubSub Client Library code时,我尝试对发布/订阅客户端库执行相同的操作:
from google.cloud.pubsub_v1.gapic.publisher_client import PublisherClient
publisher = PublisherClient.from_service_account_file(filename='PATH/service_account.json')
并出现以下错误:
回溯(最近一次通话最后一次):文件“”,第1行,在
from_service_account_file中的文件“ /[VIRT_ENV_PATH]/lib/python3.6/site-packages/google/cloud/pubsub_v1/gapic/publisher_client.py”第78行return cls(* args,** kwargs)>
文件“ /[VIRT_ENV_PATH]/lib/python3.6/site-packages/google/cloud/pubsub_v1/gapic/publisher_client.py”,第167行,位于 init self.iam_policy_stub =( iam_policy_pb2.IAMPolicyStub(channel))
文件 “ /[VIRT_ENV_PATH]/lib/python3.6/site-packages/google/iam/v1/iam_policy_pb2.py”,第344行,位于 init self.SetIamPolicy = channel.unary_unary(AttributeError:' NoneType'对象没有属性'unary_unary'
如果我想使用密钥.json文件,则只能使用变通方法使它起作用,
cred = `service_account.Credentials.from_service_account_file(filename = 'PATH/service_account.json')
publisher = pubsub_v1.PublisherClient(credentials = cred)
提前感谢您。
答案 0 :(得分:1)
据我所知,我们可以通过三种不同的方式来做到这一点:
如果您要直接指定key.json文件而不是setting the environment variable GOOGLE_APPLICATION_CREDENTIALS as specified in docs...,则为:
import os
import google.cloud.pubsub_v1 as pub
os.environ['GOOGLE_APPLICATION_CREDENTIALS']='.path/auth.json'
publisher = pub.PublisherClient()
此外,您可以使用解决方法:
import google.cloud.pubsub_v1 as pub
from google.cloud.pubsub_v1.gapic.publisher_client import PublisherClient
cred = service_account.Credentials.from_service_account_file(filename = './auth.json') #or just from_service_account_file('./auth.json')
publisher = pub.PublisherClient(credentials = cred)
但是关于第三种方式...
from google.cloud.pubsub_v1.gapic.publisher_client import PublisherClient #VERSION 1
publisher = PublisherClient.from_service_account_file('./auth.json')
#which raises the same error than:
#publisher = PublisherClient('./auth.json')
... a GitHub issue should be opened,因为它与提供none channel in the constructor, line 101有关,并且这不是预期的行为。 您是第一次做对了。