-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_app.py
More file actions
60 lines (50 loc) · 1.35 KB
/
github_app.py
File metadata and controls
60 lines (50 loc) · 1.35 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import jwt
import requests
import time
import sys
# Get PEM file path
if len(sys.argv) > 1:
pem = sys.argv[1]
else:
pem = input("Enter path of private PEM file: ")
# Get the App ID
if len(sys.argv) > 2:
app_id = sys.argv[2]
else:
app_id = input("Enter your APP ID: ")
# Open PEM
with open(pem, 'rb') as pem_file:
signing_key = jwt.jwk_from_pem(pem_file.read())
payload = {
# Issued at time
'iat': int(time.time()),
# JWT expiration time (10 minutes maximum)
'exp': int(time.time()) + 600,
# GitHub App's identifier
'iss': app_id
}
# Create JWT
jwt_instance = jwt.JWT()
encoded_jwt = jwt_instance.encode(payload, signing_key, alg='RS256')
print(f"JWT: {encoded_jwt}")
#####
installation_id = requests.get(
"https://api.github.com/app/installations",
headers={
"Accept": "application/vnd.github.+json",
"Authorization": f"Bearer {encoded_jwt}",
"X-GitHub-Api-Version": "2022-11-28",
},
)
print(installation_id.json()[0]['id'])
installation_token = requests.post(
f"https://api.github.com/app/installations/{installation_id.json()[0]['id']}/access_tokens",
headers={
"Accept": "application/vnd.github.+json",
"Authorization": f"Bearer {encoded_jwt}",
"X-GitHub-Api-Version": "2022-11-28",
},
)
print(installation_token.json())
#####