Feat: Client should pass client_options.api_key to auth library by andrewsg · Pull Request #321 · googleapis/python-cloud-core · GitHub
Skip to content
This repository was archived by the owner on Feb 13, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions .kokoro/docker/docs/requirements.txt
18 changes: 18 additions & 0 deletions google/cloud/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
from google.cloud._helpers import _determine_default_project
from google.oauth2 import service_account

try:
import google.auth.api_key

HAS_GOOGLE_AUTH_API_KEY = True
except ImportError:
HAS_GOOGLE_AUTH_API_KEY = False


_GOOGLE_AUTH_CREDENTIALS_HELP = (
"This library only supports credentials from google-auth-library-python. "
Expand Down Expand Up @@ -161,6 +168,15 @@ def __init__(self, credentials=None, _http=None, client_options=None):
"'credentials' and 'client_options.credentials_file' are mutually exclusive."
)

if (
HAS_GOOGLE_AUTH_API_KEY
and client_options.api_key
and (credentials or client_options.credentials_file)
):
raise google.api_core.exceptions.DuplicateCredentialArgs(
"'client_options.api_key' is mutually exclusive with 'credentials' and 'client_options.credentials_file'."
)

if credentials and not isinstance(
credentials, google.auth.credentials.Credentials
):
Expand All @@ -174,6 +190,8 @@ def __init__(self, credentials=None, _http=None, client_options=None):
credentials, _ = google.auth.load_credentials_from_file(
client_options.credentials_file, scopes=scopes
)
elif HAS_GOOGLE_AUTH_API_KEY and client_options.api_key is not None:
credentials = google.auth.api_key.Credentials(client_options.api_key)
else:
credentials, _ = google.auth.default(scopes=scopes)

Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_client.py