-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
43 lines (32 loc) · 1.34 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from urllib.parse import urlparse
import urllib
import argparse
import pyperclip
import google.auth.transport.requests
import google.oauth2.id_token
import os
parser = argparse.ArgumentParser()
parser.add_argument("--url", help="Cloud Run Service URL")
parser.add_argument("--credentials", help="Service account file path")
args = parser.parse_args()
# Service account key path
credential_path = args.credentials
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
def make_authorized_get_request():
"""
make_authorized_get_request makes a GET request to the specified HTTP endpoint
by authenticating with the ID token obtained from the google-auth client library
using the specified audience value.
"""
# Cloud Run uses your service's hostname as the `audience` value
audience = args.url.replace(urlparse(args.url).path, "")
# For Cloud Run, `endpoint` is the URL (hostname + path) receiving the request
endpoint = args.url
req = urllib.request.Request(endpoint)
auth_req = google.auth.transport.requests.Request()
id_token = google.oauth2.id_token.fetch_id_token(auth_req, audience)
req.add_header("Authorization", f"Bearer {id_token}")
os.system('clear')
pyperclip.copy(f"Bearer {id_token}")
print(f"Bearer {id_token} \n\n Copied to clipboard")
make_authorized_get_request()