【问题标题】:How to fix "if no http is provided, credentials must exist" issue for Google Cloud API?如何解决 Google Cloud API 的“如果未提供 http,则凭据必须存在”问题?
【发布时间】:2020-10-05 15:29:35
【问题描述】:

我是编程新手,一直在尝试使用 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()

【问题讨论】:

标签: python google-api google-translate


【解决方案1】:

我尝试了使用最新更新的库的示例,它对我有用。

不过,如果推断出的凭据没有得到妥善处理,您可以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 中创建显式凭据的更多详细信息(此答案几乎是该文档上说明的副本)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 2021-07-17
    • 2021-02-16
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多