Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit f30b45a

Browse files
fix: no warning if quota_project_id is given (#537)
If user account cred has 'quota_project_id', ignore the warning. Implementing http://shortn/_YUlAgzL40H
1 parent b74168b commit f30b45a

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

google/auth/_default.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@
4949
# Warning when using Cloud SDK user credentials
5050
_CLOUD_SDK_CREDENTIALS_WARNING = """\
5151
Your application has authenticated using end user credentials from Google \
52-
Cloud SDK. We recommend that most server applications use service accounts \
53-
instead. If your application continues to use end user credentials from Cloud \
54-
SDK, you might receive a "quota exceeded" or "API not enabled" error. For \
55-
more information about service accounts, see \
56-
https://cloud.google.com/docs/authentication/"""
52+
Cloud SDK without a quota project. You might receive a "quota exceeded" \
53+
or "API not enabled" error. We recommend you rerun \
54+
`gcloud auth application-default login` and make sure a quota project is \
55+
added. Or you can use service accounts instead. For more information \
56+
about service accounts, see https://cloud.google.com/docs/authentication/"""
5757

5858

5959
def _warn_about_problematic_credentials(credentials):
@@ -114,8 +114,8 @@ def _load_credentials_from_file(filename):
114114
msg = "Failed to load authorized user credentials from {}".format(filename)
115115
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
116116
six.raise_from(new_exc, caught_exc)
117-
# Authorized user credentials do not contain the project ID.
118-
_warn_about_problematic_credentials(credentials)
117+
if not credentials.quota_project_id:
118+
_warn_about_problematic_credentials(credentials)
119119
return credentials, None
120120

121121
elif credential_type == _SERVICE_ACCOUNT_TYPE:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"client_id": "764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com",
3+
"client_secret": "secret",
4+
"refresh_token": "alabalaportocala",
5+
"type": "authorized_user",
6+
"quota_project_id": "quota_project_id"
7+
}

tests/test__default.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
DATA_DIR, "authorized_user_cloud_sdk.json"
3838
)
3939

40+
AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE = os.path.join(
41+
DATA_DIR, "authorized_user_cloud_sdk_with_quota_project_id.json"
42+
)
43+
4044
SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json")
4145

4246
with open(SERVICE_ACCOUNT_FILE) as fh:
@@ -101,6 +105,13 @@ def test__load_credentials_from_file_authorized_user_cloud_sdk():
101105
assert isinstance(credentials, google.oauth2.credentials.Credentials)
102106
assert project_id is None
103107

108+
# No warning if the json file has quota project id.
109+
credentials, project_id = _default._load_credentials_from_file(
110+
AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE
111+
)
112+
assert isinstance(credentials, google.oauth2.credentials.Credentials)
113+
assert project_id is None
114+
104115

105116
def test__load_credentials_from_file_service_account():
106117
credentials, project_id = _default._load_credentials_from_file(SERVICE_ACCOUNT_FILE)

0 commit comments

Comments
 (0)