-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
38 lines (31 loc) · 1006 Bytes
/
main.py
File metadata and controls
38 lines (31 loc) · 1006 Bytes
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
import datetime
import os
import sys
import requests
import jwt
gh_app_id = os.getenv('GH_APP_ID')
gh_app_key = os.getenv('GH_APP_KEY')
gh_app_inst_id = os.getenv('GH_APP_INST_ID')
if __name__ == "__main__":
if not gh_app_id:
sys.exit("GH_APP_ID is not set")
if not gh_app_key:
sys.exit("GH_APP_KEY is not set")
with open(gh_app_key, "r") as key_file:
key = key_file.read()
now = int(datetime.datetime.now().timestamp())
payload = {
"iat": now - 60,
"exp": now + 60 * 8, # expire after 8 minutes
"iss": gh_app_id
}
encoded = jwt.encode(payload=payload, key=key, algorithm="RS256")
if not gh_app_inst_id:
print(encoded.decode())
else:
url = f"https://api.github.com/app/installations/{gh_app_inst_id}/access_tokens"
headers = {
"Authorization": f"Bearer {encoded.decode()}"
}
response = requests.post(url, headers=headers)
print(response.json()["token"])