Skip to content

Instantly share code, notes, and snippets.

@ibrezm1
Created April 16, 2022 13:32
Show Gist options
  • Select an option

  • Save ibrezm1/1a73c6e5336e1fbc4f14e2404e8d3e10 to your computer and use it in GitHub Desktop.

Select an option

Save ibrezm1/1a73c6e5336e1fbc4f14e2404e8d3e10 to your computer and use it in GitHub Desktop.
# Sample Cloud execution on local
# pip install google-auth
# pip install --upgrade google-auth
import os
import json
import requests
import google.oauth2.id_token
import google.auth.transport.requests
function_url = 'https://us-central1-zeta-yen-319702.cloudfunctions.net/function-1'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = './zeta-yen-319702-8ea3f83d2b69.json'
request = google.auth.transport.requests.Request()
audience = function_url
TOKEN = google.oauth2.id_token.fetch_id_token(request, audience)
r = requests.post(
function_url,
headers={'Authorization': f"Bearer {TOKEN}", "Content-Type": "application/json"},
data=json.dumps({"name": "test"}) # possible request parameters
)
print (r.status_code, r.json() )
#curl -m 70 -X POST https://us-central1-zeta-yen-319702.cloudfunctions.net/function-1 \
#-H "Authorization:bearer $(gcloud auth print-identity-token)" \
#-H "Content-Type:application/json" \
#-d '{
# "name": "Hello World"
#}'
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment