无法在 python 中对 Google Cloud Storage 客户端进行身份验证

Posted

技术标签:

【中文标题】无法在 python 中对 Google Cloud Storage 客户端进行身份验证【英文标题】:Unable to authenticate Google Cloud Storage client in python 【发布时间】:2017-08-12 01:35:23 【问题描述】:

我正在尝试使用以下代码访问 Google Cloud 存储:

client = storage.Client(credentials = GoogleCredentials.from_stream(<path-to-service-account-json>))

抛出此错误:

ValueError: This library only supports credentials from google-auth-library-python. See https://google-cloud-python.readthedocs.io/en/latest/google-cloud-auth.html for help on authentication with this library.

我能够使用相同的凭据(使用 GoogleCredentials.from_stream() 获得)对 discovery.build 服务进行身份验证,但由于某种原因 storage.Client 无法使用提供的凭据。

如果我遗漏了什么,请告诉我。

【问题讨论】:

【参考方案1】:

让它与以下代码一起工作:

storage_client = storage.Client.from_service_account_json(<path-to-service-account-json>)

不知道为什么有不同的方式来验证发现 API 以及存储和其他客户端 API。

【讨论】:

个别 API 使用 Client.from_service_account_json('/path/to/keyfile.json') 模式。 GoogleCredentials 类是旧库的一部分。使用 google-auth-library-python 显式使用 Credentials 对象的方法看起来更像 credentials = service_account.Credentials.from_service_account_file('/path/to/keyfile.json')。有关该方法的更多文档:google-auth.readthedocs.io/en/latest/user-guide.html 您可能正在使用确实使用 GoogleCredentials 的旧 Python API 进行发现。很抱歉造成混乱。

以上是关于无法在 python 中对 Google Cloud Storage 客户端进行身份验证的主要内容,如果未能解决你的问题,请参考以下文章