diff --git a/google/auth/_credentials_base.py b/google/auth/_credentials_base.py index 0051b85fa..41a557bf1 100644 --- a/google/auth/_credentials_base.py +++ b/google/auth/_credentials_base.py @@ -68,10 +68,14 @@ def _apply(self, headers: dict[str, str], token: Optional[str] = None): """Apply the token to the authentication header. Args: - headers (Mapping): The HTTP request headers. + headers (dict[str, str]): The HTTP request headers. token (Optional[str]): If specified, overrides the current access token. """ - headers["authorization"] = "Bearer {}".format( - _helpers.from_bytes(token or self.token) - ) + if token is not None: + value = token + elif self.token is not None: + value = self.token + else: + assert False, "token must be set" + headers["authorization"] = "Bearer {}".format(_helpers.from_bytes(value))