Report #46531
[bug\_fix] Google Cloud SDK returns 'DefaultCredentialsError: Could not automatically determine credentials' because Application Default Credentials \(ADC\) are not found in the environment
Run \`gcloud auth application-default login\` to generate local ADC credentials for development, which stores credentials in \`~/.config/gcloud/application\_default\_credentials.json\`. Alternatively, set the \`GOOGLE\_APPLICATION\_CREDENTIALS\` environment variable to the absolute path of a service account JSON key file. In production on GCP, ensure the resource \(Compute Engine, Cloud Run, GKE\) has a service account attached, as the client libraries will automatically retrieve tokens from the metadata server \(169.254.169.254\) via the ADC chain.
Journey Context:
Developer writes a Python script using \`google-cloud-storage\` to upload files. It works perfectly in Cloud Shell. They clone the repo to their MacBook, install dependencies, and run the script. It immediately fails with 'DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE\_APPLICATION\_CREDENTIALS or explicitly create credentials'. Developer checks \`gcloud config list\` and sees they are authenticated to the correct project. They try \`gcloud auth print-access-token\` and it returns a valid token. They search the error and find that \`gcloud auth login\` authenticates the CLI tool, but client libraries use a different mechanism called Application Default Credentials \(ADC\). They read that ADC looks in specific places: first the env var, then a well-known path. They run \`gcloud auth application-default login\`, which opens a browser, they select their Google account, and the command completes. A file appears at \`~/.config/gcloud/application\_default\_credentials.json\`. They run the Python script again and it works. Later, when deploying to Cloud Run, they unset the env var and let the service use its attached service account via the metadata server, understanding that ADC automatically handles the token refresh in that environment.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T08:34:33.119290+00:00— report_created — created