Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

Google Cloud Function :: Service account :: JWT token and Bearer token

I have a Google Cloud Function. I also have a web application. I want to authenticate requests to the cloud function by using a service account.

I have the json key file.

I know that I have to follow https://cloud.google.com/functions/docs/securing/authenticating#service-to-function. But that is leading me to an IAP page that does not apply to google cloud functions.

Another similar instructions are found in https://developers.google.com/identity/protocols/oauth2/service-account

But if I am following the python library code, I end up with the sample code there :

import googleapiclient.discovery

sqladmin = googleapiclient.discovery.build('sqladmin', 'v1beta3', credentials=credentials)


response = sqladmin.instances().list(project='exciting-example-123').execute()

This does not directly relate to invoking a cloud function.

This question's answer somewhat deals with my requirement but is using a Call API which is only suitable for testing.

Also, I want to expose this API to multiple applications using another tech like .net. So I believe the best option for me will be to use the HTTP method (given on the same page):

https://developers.google.com/identity/protocols/oauth2/service-account#httprest

But whatever I do I am unable to get the signature right.

Any help to get this sorted will be highly appreciated as I am stuck on this for the past few days.

question from:https://stackoverflow.com/questions/65847421/google-cloud-function-service-account-jwt-token-and-bearer-token

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the Google auth library like this


from google.oauth2.id_token import fetch_id_token
from google.auth.transport import requests

audience="my_audience"
r = requests.Request()

token=fetch_id_token(r,audience)

print(token)

The fetch_id_token method will use the default credentials

  1. The service account key file defined in the environment variable GOOGLE_APPLICATION_CREDENTIALS
  2. The service account loaded in the Google Cloud environment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...