Unable to authenticate Google Cloud Storage client in python
Asked Answered
M

1

21

I'm trying the following code to access the Google Cloud storage:

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

Throws this error:

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.

I'm able to authenticate discovery.build service using the same credentials(obtained using GoogleCredentials.from_stream()), but for some reason storage.Client fails to use the provided credentials.

Please let me know if I'm missing something.

Masbate answered 12/8, 2017 at 1:35 Comment(0)
M
38

Got it to work with the following code:

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

Not sure why there are different way to authenticate the discovery API and the storage and other client APIs.

Masbate answered 14/8, 2017 at 18:5 Comment(2)
Individual APIs use the pattern Client.from_service_account_json('/path/to/keyfile.json'). The GoogleCredentials class is part of an older library. The way to explicitly use a Credentials object with google-auth-library-python would look more like credentials = service_account.Credentials.from_service_account_file('/path/to/keyfile.json'). More docs on that method: google-auth.readthedocs.io/en/latest/user-guide.html You're probably doing discovery with that older Python API, which does use GoogleCredentials. Sorry for the confusion.Mycorrhiza
Thank you, can't find any useful docs for this client library so your post is extra helpful.Mcalpine

© 2022 - 2024 — McMap. All rights reserved.