Ok, I WAS getting it wrong*, according to the docs here -
The recommended way to use Google APIs is to use a client library and
Application Default Credentials (ADC).
This works in a local development environment as well.
ADC searches for credentials in the following locations (in this order)
- GOOGLE_APPLICATION_CREDENTIALS environment variable
- User credentials set up by using the Google Cloud CLI
- The attached service account, returned by the metadata server
When logging in with gcloud auth application-default login
, this command puts your credentials into a well-known location for use by ADC in a local development environment (e.g. ~/.config/gcloud/application_default_credentials.json
). These are your local ADC credentials.
The GOOGLE_APPLICATION_CREDENTIALS
is NOT set automatically and is not needed, in most cases.
The credentials you provide to ADC by using the gcloud CLI (with gcloud auth application-default login
as mentioned above) are distinct from your gcloud credentials — the credentials the gcloud CLI uses to authenticate to Google Cloud (i.e. with gcloud auth login
, and using named/topic configurations using gcloud config configurations activate <env>
). These are used to authenticate to and authorize access to Google Cloud services.
Your local ADC credentials are not used by the gcloud CLI, and your
gcloud credentials are not used by ADC. They are two distinct sets of
credentials.
Fetching ID token for authenticated requests
Ok, So we had local ADC credentials, and we had gcloud credentials. But neither of them will work with google.oauth2.id_token.fetch_id_token
locally. For this, you'll need to use credentials of a service-account. you can get them using the web console or CLI, in short it's a json file, similar to the one that is created upon logging in with gcloud auth application-default login
, but of a service-account. In order to make it work, you'll need to direct ADC to use this file - e.g. setting GOOGLE_APPLICATION_CREDENTIALS
to this json file's path.
* In my defense I can confirm that google.cloud.firestore.Client()
library works very well with gcloud credentials, including with different named configurations, which was the thing that really confused me. I'm still not sure why it works, as opposed to others like google.cloud.storage.Client()
, which only works with ADC credentials locally.