You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sample code below provided under MIT and Apache 2.0 licenses. This runs and works on your computer if you install the gcloud cli and run gcloud auth login
importasyncioimportthreadingimportgoogle.auth.transport.requestsimportgoogle.authimporthttpxclassGoogleAuth(httpx.Auth):
"""Adds required authorization for requests to Google Cloud Platform. This gets the default credentials for the running user, and uses them to generate valid tokens to attach to requests. """def__init__(self, scopes=("https://www.googleapis.com/auth/cloud-platform",)):
self._sync_lock=threading.RLock()
self._async_lock=asyncio.Lock()
self.scopes=scopesself.creds=Nonedef_refresh_creds(self):
# Must only be called with a lock.ifself.credsisNone:
self.creds, _=google.auth.default(scopes=self.scopes)
auth_req=google.auth.transport.requests.Request()
self.creds.refresh(auth_req)
defsync_auth_flow(self, request: httpx.Request):
ifself.credsisNoneorself.creds.expired:
withself._sync_lock:
self._refresh_creds()
request.headers["Authorization"] ="Bearer "+self.creds.tokenyieldrequestasyncdefasync_auth_flow(self, request: httpx.Request):
ifself.credsisNoneorself.creds.expired:
asyncwithself._async_lock:
awaitasyncio.to_thread(self._refresh_creds)
request.headers["Authorization"] ="Bearer "+self.creds.tokenyieldrequestclient=httpx.Client(auth=GoogleAuth())
response=client.get("https://cloudresourcemanager.googleapis.com/v1/projects")
print(response.json())
The text was updated successfully, but these errors were encountered:
Sample code below provided under MIT and Apache 2.0 licenses. This runs and works on your computer if you install the gcloud cli and run
gcloud auth login
The text was updated successfully, but these errors were encountered: