无法在python中验证Google云端存储客户端

Fay*_*med 2 google-cloud-storage google-cloud-platform

我正在尝试以下代码来访问Google云端存储:

client = storage.Client(credentials = GoogleCredentials.from_stream(<path-to-service-account-json>))
Run Code Online (Sandbox Code Playgroud)

抛出此错误:

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.
Run Code Online (Sandbox Code Playgroud)

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

如果我遗失了什么,请告诉我.

Fay*_*med 12

得到它使用以下代码:

storage_client = storage.Client.from_service_account_json(<path-to-service-account-json>)
Run Code Online (Sandbox Code Playgroud)

不确定为什么有不同的方法来验证发现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')`.有关该方法的更多文档:https://google-auth.readthedocs.io/en/latest/user-guide.html您可能正在使用旧的Python API进行发现,该API使用GoogleCredentials.对困惑感到抱歉. (3认同)