@@ -123,26 +123,40 @@ class ApplicationDefault(Base):
123123 """A Google Application Default credential."""
124124
125125 def __init__ (self ):
126- """Initializes the Application Default credentials for the current environment .
126+ """Creates an instance that will use Application Default credentials .
127127
128- Raises:
129- google.auth.exceptions.DefaultCredentialsError: If Application Default
130- credentials cannot be initialized in the current environment.
128+ The credentials will be lazily initialized when get_credential() or
129+ project_id() is called. See those methods for possible errors raised.
131130 """
132131 super (ApplicationDefault , self ).__init__ ()
133- self ._g_credential , self . _project_id = google . auth . default ( scopes = _scopes )
132+ self ._g_credential = None # Will be lazily-loaded via _load_credential().
134133
135134 def get_credential (self ):
136135 """Returns the underlying Google credential.
137136
137+ Raises:
138+ google.auth.exceptions.DefaultCredentialsError: If Application Default
139+ credentials cannot be initialized in the current environment.
138140 Returns:
139141 google.auth.credentials.Credentials: A Google Auth credential instance."""
142+ self ._load_credential ()
140143 return self ._g_credential
141144
142145 @property
143146 def project_id (self ):
147+ """Returns the project_id from the underlying Google credential.
148+
149+ Raises:
150+ google.auth.exceptions.DefaultCredentialsError: If Application Default
151+ credentials cannot be initialized in the current environment.
152+ Returns:
153+ str: The project id."""
154+ self ._load_credential ()
144155 return self ._project_id
145156
157+ def _load_credential (self ):
158+ if not self ._g_credential :
159+ self ._g_credential , self ._project_id = google .auth .default (scopes = _scopes )
146160
147161class RefreshToken (Base ):
148162 """A credential initialized from an existing refresh token."""
@@ -199,19 +213,3 @@ def get_credential(self):
199213 Returns:
200214 google.auth.credentials.Credentials: A Google Auth credential instance."""
201215 return self ._g_credential
202-
203-
204- class FakeCredential (Base ):
205- """Provides fake credentials, which is only accepted in local emulators."""
206-
207- def get_credential (self ):
208- return _EmulatorAdminCredentials ()
209-
210-
211- class _EmulatorAdminCredentials (google .auth .credentials .Credentials ):
212- def __init__ (self ):
213- google .auth .credentials .Credentials .__init__ (self )
214- self .token = 'owner'
215-
216- def refresh (self , request ):
217- pass
0 commit comments