GeezerVison is showing github.com — Infer project from credentials if not passed explicitly · Issue #27 · googleapis/python-cloud-core
This repository was archived by the owner on Feb 13, 2026. It is now read-only.
This repository was archived by the owner on Feb 13, 2026. It is now read-only.
Infer project from credentials if not passed explicitly #27DescriptionSplit off from #17
See also: googleapis/python-datastore#74
See also: googleapis/python-storage#177
In a "blank slate" design, I would argue that the credentials' project should supersede the one present in the environment. However, for backward compatibility, we need to use the credentials' project only if the environment variable is not set. So the order should follow the pattern from google.auth.default when inferring project ID:
- Explicit project ID passed to client constructor
- Project ID set in
GOOGLE_CLOUD_PROJECT envvar
- Project ID from credentials, if present
- Project ID from SDK
- Project ID from GAE
- Project ID from GCE
Activitytseaver commented on Dec 8, 2020 ContributorAuthor More actions In the case where credentials are discovered for us, google.auth.default() already falls back to the project in the credentials. E.g.:
$ unset GOOGLE_CLOUD_PROJECT GCLOUD_PROJECT
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
$ grep project_id /path/to/service_account.json
"project_id": "some-project-123"
$ python
>>> from google.auth import default
>>> credentials, project = default()
>>> project
'some-project-123'
>>> from google.cloud.client import ClientWithProject
>>> client = ClientWithProject()
>>> client.project
'some-project-123'
So, the case we need to cover for this feature is when the user has passed in explicit service account credentials, but not an explicit project ID, e.g. (this won't currently work):
$ unset GOOGLE_CLOUD_PROJECT GCLOUD_PROJECT GOOGLE_APPLICATION_CREDENTIALS
$ python
>>> from google.oauth2.service_account import Credentials
>>> credentials = Credentials.from_service_account_file("/path/to/service_account.json")
>>> from google.cloud.client import ClientWithProject
>>> client = ClientWithProject(credentials=credentials) # currently raisesadded a commit that references this issue on Dec 8, 2020This issue will close once commit 626ae7e is merged into the 'main' branch.626ae7e added a commit that references this issue on Jan 19, 2021This issue will close once commit 2bd41a1 is merged into the 'main' branch.2bd41a1 dhermes commented on Oct 7, 2024 Is there a reason CLOUDSDK_CORE_PROJECT isn't also supported?
I made it here via the comment near a stack-trace I followed:
|
# This test duplicates the one from `google.auth.default`, but earlier, |
|
# for backward compatibility: we want the environment variable to |
|
# override any project set on the credentials. See: |
|
# https://github.com/googleapis/python-cloud-core/issues/27 |
|
if project is None: |
|
project = os.getenv( |
|
environment_vars.PROJECT, |
|
os.getenv(environment_vars.LEGACY_PROJECT), |
|
) |
Update: I just did some testing and confirmed it is supported. Not sure what sort of heisenbug I was observing!
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|