-
-
Save stevepeak/2d6274bfc4c8570e7a0b49611cef1888 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python ghi_test.py 254 123 owner/repo /contents/readme.md | |
# python ghi_test.py integration_id installation_id slug ?endpoint | |
import os | |
import jwt | |
import sys | |
import requests | |
from time import time | |
pem_file = os.path.join(os.getcwd(), 'src/certs/github.pem') | |
integration_id = sys.argv[1] | |
installation_id = sys.argv[2] | |
expire_seconds = 500 | |
slug = sys.argv[3] | |
endpoint = sys.argv[4] if len(sys.argv) > 4 else '' | |
with open(pem_file) as p: | |
pem = p.read() | |
# Step 1) Create an integrations access token | |
now = int(time())-1 | |
payload = {'iat': now, 'exp': now + expire_seconds, 'iss': integration_id} | |
token = jwt.encode(payload, pem, algorithm='RS256') | |
headers = {'Accept': 'application/vnd.github.machine-man-preview+json', 'Authorization': 'Bearer '+token} | |
res = requests.post('https://api.github.com/installations/%s/access_tokens' % installation_id, headers=headers) | |
result = res.json() | |
print dict(code=res.status_code, headers=res.headers, body=result) | |
# Step 2) use token to interact with github api | |
headers['Authorization'] = 'Bearer %s' % result['token'] | |
res = requests.get('https://api.github.com/repos/%s%s' % (slug, endpoint), headers=headers) | |
print dict(code=res.status_code, headers=res.headers, body=res.json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment