'OAuth2 authentication for a Cloud function
I need to invoke (OAuth - Client credentials flow and Authorization Code flow) an http cloud function from python code written in my local.
I have created OAuth 2.0 Client IDs on the GCP project.
Below code is used to get an access token but this is go to google login page with an error. Can we some please help to understand how to invoke a cloud function from local python code ? any examples or guidance will be every helpful.
from google_auth_oauthlib import flow
oauthcredential= "client_secret.json" # OAuth 2.0 Client ID JSON
launch_browser = True # when using locally and False when remote
appflow = flow.InstalledAppFlow.from_client_secrets_file(oauthcredential,scopes=['https://www.googleapis.com/auth/userinfo.profile'])
if launch_browser:
appflow.run_local_server()
else:
appflow.run_console()
credentials = appflow.credentials
Solution 1:[1]
To invoke a protected Cloud Function, you need an identity token and not an access token. You can't generate an id token from your own user credential in your code (only with the gcloud command line) and you use a service account key file to achieve this (that is not ideal, as I explain in this article).
In your code, set the GOOGLE_APPLICATION_CREDENTIALS environment variable that reference the path of your service account key file and then you can use the fetch_id_token function to get an id token
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | guillaume blaquiere |
