Report #52670
[bug\_fix] DefaultCredentialsError: Could not automatically determine credentials when using Google Cloud client libraries
Set the environment variable GOOGLE\_APPLICATION\_CREDENTIALS to the path of a service account JSON key file, or run \`gcloud auth application-default login\` to generate local ADC credentials. The root cause is that Application Default Credentials \(ADC\) follows a strict search order: first the GOOGLE\_APPLICATION\_CREDENTIALS env var, then the gcloud ADC file at ~/.config/gcloud/application\_default\_credentials.json, then the metadata server \(only available on GCP compute\). Local machines and non-GCP containers lack the metadata server, causing the chain to fail.
Journey Context:
Developer writes a Python script using \`google-cloud-storage\` to upload files. It runs perfectly on a GCP Compute Engine VM. They clone the repository to their local macOS laptop, install dependencies via pip, and run the script. It immediately raises \`google.auth.exceptions.DefaultCredentialsError\`. They check \`gcloud auth list\` and see they are logged in with their user account, assuming that should be sufficient. They try setting \`GOOGLE\_CLOUD\_PROJECT\` env var, but the error persists. They search the error message and find documentation mentioning 'Application Default Credentials'. They learn that \`gcloud auth login\` authenticates the gcloud CLI itself, but not the Application Default Credentials used by client libraries. They have two options: download a service account key JSON from the GCP Console and set \`GOOGLE\_APPLICATION\_CREDENTIALS=/path/to/key.json\`, or run \`gcloud auth application-default login\` which opens a browser, obtains an OAuth refresh token, and writes it to \`~/.config/gcloud/application\_default\_credentials.json\`. After running the latter, the script works locally because the ADC library \(google-auth\) explicitly looks for that file path in its search chain.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T18:54:18.103178+00:00— report_created — created