Skip to content

Instantly share code, notes, and snippets.

@zemmyang
Created November 23, 2022 15:44
Show Gist options
  • Save zemmyang/b7bb7c27356da2bbf8e077acc498fe0c to your computer and use it in GitHub Desktop.
Save zemmyang/b7bb7c27356da2bbf8e077acc498fe0c to your computer and use it in GitHub Desktop.
Send HTTP request from one Google Cloud Function to another HTTP-triggered Google Cloud Function
# the HTTP-triggered GCF does not need to have "Allow Unauthenticated" set
import urllib
import json
import google.auth.transport.requests
import google.oauth2.id_token
url = <YOUR URL>
payload = {
"<KEY>": "<VALUE>"
}
data = json.dumps(payload)
data = str(data)
data = data.encode('utf-8')
req = urllib.request.Request(url, data)
auth_req = google.auth.transport.requests.Request()
id_token = google.oauth2.id_token.fetch_id_token(auth_req, url)
req.add_header("Authorization", f"Bearer {id_token}")
req.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(req)
print(response.code)
print(response.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment