4949from google .auth import environment_vars
5050from google .auth import exceptions
5151from google .auth import transport
52+ from google .oauth2 import service_account
5253
5354_LOGGER = logging .getLogger (__name__ )
5455
@@ -262,6 +263,9 @@ def my_cert_callback():
262263 retried.
263264 max_refresh_attempts (int): The maximum number of times to attempt to
264265 refresh the credentials and retry the request.
266+ default_host (Optional[str]): A host like "pubsub.googleapis.com".
267+ This is used when a self-signed JWT is created from service
268+ account credentials.
265269 """
266270
267271 def __init__ (
@@ -270,6 +274,7 @@ def __init__(
270274 http = None ,
271275 refresh_status_codes = transport .DEFAULT_REFRESH_STATUS_CODES ,
272276 max_refresh_attempts = transport .DEFAULT_MAX_REFRESH_ATTEMPTS ,
277+ default_host = None ,
273278 ):
274279 if http is None :
275280 self .http = _make_default_http ()
@@ -281,10 +286,22 @@ def __init__(
281286 self .credentials = credentials
282287 self ._refresh_status_codes = refresh_status_codes
283288 self ._max_refresh_attempts = max_refresh_attempts
289+ self ._default_host = default_host
284290 # Request instance used by internal methods (for example,
285291 # credentials.refresh).
286292 self ._request = Request (self .http )
287293
294+ # https://google.aip.dev/auth/4111
295+ # Attempt to use self-signed JWTs when a service account is used.
296+ # A default host must be explicitly provided.
297+ if (
298+ isinstance (self .credentials , service_account .Credentials )
299+ and self ._default_host
300+ ):
301+ self .credentials ._create_self_signed_jwt (
302+ "https://{}/" .format (self ._default_host )
303+ )
304+
288305 super (AuthorizedHttp , self ).__init__ ()
289306
290307 def configure_mtls_channel (self , client_cert_callback = None ):
0 commit comments