2525import google .api_core .exceptions
2626import google .auth
2727from google .auth import environment_vars
28- import google .auth .api_key
2928import google .auth .credentials
3029import google .auth .transport .requests
3130from google .cloud ._helpers import _determine_default_project
3231from google .oauth2 import service_account
3332
33+ try :
34+ import google .auth .api_key
35+
36+ HAS_GOOGLE_AUTH_API_KEY = True
37+ except :
38+ HAS_GOOGLE_AUTH_API_KEY = False
39+
3440
3541_GOOGLE_AUTH_CREDENTIALS_HELP = (
3642 "This library only supports credentials from google-auth-library-python. "
@@ -162,7 +168,11 @@ def __init__(self, credentials=None, _http=None, client_options=None):
162168 "'credentials' and 'client_options.credentials_file' are mutually exclusive."
163169 )
164170
165- if client_options .api_key and (credentials or client_options .credentials_file ):
171+ if (
172+ HAS_GOOGLE_AUTH_API_KEY
173+ and client_options .api_key
174+ and (credentials or client_options .credentials_file )
175+ ):
166176 raise google .api_core .exceptions .DuplicateCredentialArgs (
167177 "'client_options.api_key' is mutually exclusive with 'credentials' and 'client_options.credentials_file'."
168178 )
@@ -180,7 +190,7 @@ def __init__(self, credentials=None, _http=None, client_options=None):
180190 credentials , _ = google .auth .load_credentials_from_file (
181191 client_options .credentials_file , scopes = scopes
182192 )
183- elif client_options .api_key is not None :
193+ elif HAS_GOOGLE_AUTH_API_KEY and client_options .api_key is not None :
184194 credentials = google .auth .api_key .Credentials (client_options .api_key )
185195 else :
186196 credentials , _ = google .auth .default (scopes = scopes )
0 commit comments