feat: send quota project id in x-goog-user-project for OAuth2 credentials (#412)
* feat: send quota project id in x-goog-user-project header for OAuth2 credentials (#400)
When the 3LO credentials are used, the quota project ("quota_project_id") is sent on every outgoing request in the x-goog-user-project HTTP header/grpc metadata. The quota project is used for billing and quota purposes.
* feat: add `__setstate__` and `__getstate__` to `oauth2.credentials` class
diff --git a/google/oauth2/credentials.py b/google/oauth2/credentials.py
index 3a32c06..71d2f61 100644
--- a/google/oauth2/credentials.py
+++ b/google/oauth2/credentials.py
@@ -58,6 +58,7 @@
client_id=None,
client_secret=None,
scopes=None,
+ quota_project_id=None,
):
"""
Args:
@@ -81,6 +82,9 @@
token if refresh information is provided (e.g. The refresh
token scopes are a superset of this or contain a wild card
scope like 'https://www.googleapis.com/auth/any-api').
+ quota_project_id (Optional[str]): The project ID used for quota and billing.
+ This project may be different from the project used to
+ create the credentials.
"""
super(Credentials, self).__init__()
self.token = token
@@ -90,6 +94,27 @@
self._token_uri = token_uri
self._client_id = client_id
self._client_secret = client_secret
+ self._quota_project_id = quota_project_id
+
+ def __getstate__(self):
+ """A __getstate__ method must exist for the __setstate__ to be called
+ This is identical to the default implementation.
+ See https://docs.python.org/3.7/library/pickle.html#object.__setstate__
+ """
+ return self.__dict__
+
+ def __setstate__(self, d):
+ """Credentials pickled with older versions of the class do not have
+ all the attributes."""
+ self.token = d.get("token")
+ self.expiry = d.get("expiry")
+ self._refresh_token = d.get("_refresh_token")
+ self._id_token = d.get("_id_token")
+ self._scopes = d.get("_scopes")
+ self._token_uri = d.get("_token_uri")
+ self._client_id = d.get("_client_id")
+ self._client_secret = d.get("_client_secret")
+ self._quota_project_id = d.get("_quota_project_id")
@property
def refresh_token(self):
@@ -124,6 +149,11 @@
return self._client_secret
@property
+ def quota_project_id(self):
+ """Optional[str]: The project to use for quota and billing purposes."""
+ return self._quota_project_id
+
+ @property
def requires_scopes(self):
"""False: OAuth 2.0 credentials have their scopes set when
the initial token is requested and can not be changed."""
@@ -169,6 +199,12 @@
)
)
+ @_helpers.copy_docstring(credentials.Credentials)
+ def apply(self, headers, token=None):
+ super(Credentials, self).apply(headers, token=token)
+ if self.quota_project_id is not None:
+ headers["x-goog-user-project"] = self.quota_project_id
+
@classmethod
def from_authorized_user_info(cls, info, scopes=None):
"""Creates a Credentials instance from parsed authorized user info.
@@ -202,6 +238,9 @@
scopes=scopes,
client_id=info["client_id"],
client_secret=info["client_secret"],
+ quota_project_id=info.get(
+ "quota_project_id"
+ ), # quota project may not exist
)
@classmethod