Last active
May 15, 2017 06:02
-
-
Save stevepeak/2939349d21cc68024d09583a8529d07d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import jwt | |
import requests | |
from time import time | |
pem_file = '/path/to/github.pem' # the absolute path to your Application Pem Certificate issued by GitHub | |
integration_id = 0 # GitHub Application Integration ID | |
installation_id = 0 # once installed on an organization. The Organization Integration ID | |
expire_seconds = 500 # number of seconds the jwt token expires (max ~600 but not designated by GitHub) | |
slug = 'owner/repo' # name of repo for demo purposes | |
with open(pem_file) as p: | |
pem = p.read() | |
# Step 1) Create an integrations access token | |
now = int(time()) | |
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 res.status_code, 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' % slug, headers=headers) | |
print res.status_code, res.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you community!