Python 3.x 无法使用.json密钥文件创建发布/订阅发布服务器客户端
我无法使用.json文件而不是使用隐式应用程序默认值/GOOGLE_Application_CREDENTIALS env var创建发布/订阅发布服务器客户端。是我做错了还是发布/订阅有一些细微差别?在这种情况下,为什么 对于ex BigQuery,大多数Google客户端库都允许这样做Python 3.x 无法使用.json密钥文件创建发布/订阅发布服务器客户端,python-3.x,google-api,google-cloud-pubsub,Python 3.x,Google Api,Google Cloud Pubsub,我无法使用.json文件而不是使用隐式应用程序默认值/GOOGLE_Application_CREDENTIALS env var创建发布/订阅发布服务器客户端。是我做错了还是发布/订阅有一些细微差别?在这种情况下,为什么 对于ex BigQuery,大多数Google客户端库都允许这样做 client = bigquery.Client.from_service_account_json(json_credentials_path = ‘PATH/service_account.json')
client = bigquery.Client.from_service_account_json(json_credentials_path = ‘PATH/service_account.json')
通过深入研究,我尝试对Pub/Sub客户端库执行相同的操作:
from google.cloud.pubsub_v1.gapic.publisher_client import PublisherClient
publisher = PublisherClient.from_service_account_file(filename='PATH/service_account.json')
并获取以下错误:
回溯最近一次调用:文件,第1行,在
文件“/[VIRT_ENV_PATH]/lib/python3.6/site-packages/google/cloud/pubsub_v1/gapic/publisher_client.py,第78行,在from_service_account_文件中返回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.iampolicy stubchannel中
文件
/[VIRT_ENV_PATH]/lib/python3.6/site-packages/google/iam/v1/iam_policy_pb2.py,第344行,在init self.SetIamPolicy=channel.unary_一元属性error:'NoneType'对象没有属性'unary'
如果我想使用key.json文件,我只能使用变通方法使其工作
cred = `service_account.Credentials.from_service_account_file(filename = 'PATH/service_account.json')
publisher = pubsub_v1.PublisherClient(credentials = cred)
提前感谢您给我的帮助。据我所知,我们可以通过三种不同的方式来实现: 如果要直接指定key.json文件,而不是指定以下文件:
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因为它与提供有关,而这不是预期的行为。您第一次做得很好。此函数看起来像是从_service_account_filecls,filename,*args,**kwargs.请求一些cls对象定义。但是,在任何情况下,在函数内部它都与您所做的完全相同。因此,您的解决方案可能是一个很好的解决方案注意。你找到CLS代表什么了吗?嗨,Temu,这是一个@classmethod,这就是为什么CLS作为第一个参数:哇,你完全正确,我错过了。漫长的一天。很抱歉疏忽了!