如何解决如何解决Google Cloud API的“如果未提供http,则凭据必须存在”的问题?
我是编程的新手,并且一直在尝试使用Google Translate API。我遇到了这个问题,并试图找到无济于事的解决方案。我已经创建了一个Google服务帐户和密钥。我在Jupyter Notebook内的Mac上。
错误:
~/opt/anaconda3/lib/python3.7/site-packages/google/cloud/client.py in __init__(self,credentials,_http,client_options)
141 raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
142
--> 143 scopes = client_options.scopes or self.SCOPE
144
145 # if no http is provided,credentials must exist
AttributeError: 'ClientOptions' object has no attribute 'scopes'
我的代码:
import os
from google.cloud import translate_v2
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="cloud_key.json"
translate_client = translate.Client()
解决方法
我尝试了使用最新更新的库的示例,它对我有用。
但是,如果未正确处理推断出的凭据,则可以pass explicit credentials to the client:
from google.cloud import translate_v2 as translate
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('/path/to/key.json')
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/cloud-platform'])
translate_client = translate.Client(credentials=scoped_credentials)
有关在here中创建显式凭据的更多详细信息(此答案几乎是该文档上的说明的副本)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。