Python “如何修复”;如果未提供http,则凭据必须存在;谷歌云API的问题?

Python “如何修复”;如果未提供http,则凭据必须存在;谷歌云API的问题?,python,google-api,google-translate,Python,Google Api,Google Translate,我是编程新手,一直在尝试使用谷歌翻译API。我遇到了这个问题,并试图找到一个解决方案,但没有结果。我已经创建了一个谷歌服务帐户,并提供了密钥。我用的是苹果电脑内置的Jupyter笔记本 错误: ~/opt/anaconda3/lib/python3.7/site-packages/google/cloud/client.py in __init__(self, credentials, _http, client_options) 141 raise ValueE

我是编程新手,一直在尝试使用谷歌翻译API。我遇到了这个问题,并试图找到一个解决方案,但没有结果。我已经创建了一个谷歌服务帐户,并提供了密钥。我用的是苹果电脑内置的Jupyter笔记本

错误:

~/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()

我用最新更新的库尝试了这个例子,它对我很有用

但是,如果推断的凭据没有得到正确处理,您可以:

从google.cloud导入translate\u v2作为translate
从google.oauth2导入服务_帐户
credentials=service_account.credentials.from_service_account_文件('/path/to/key.json'))
作用域\u凭据=凭据。具有\u作用域(['https://www.googleapis.com/auth/cloud-platform'])
translate\u client=translate.client(凭据=范围内的\u凭据)

有关在中创建显式凭据的更多详细信息(此答案几乎是该文档上说明的副本)。

您可以检查此线程这是否回答了您的问题?