Skip to content

Instantly share code, notes, and snippets.

@stevepeak
Last active May 15, 2017 06:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevepeak/2939349d21cc68024d09583a8529d07d to your computer and use it in GitHub Desktop.
Save stevepeak/2939349d21cc68024d09583a8529d07d to your computer and use it in GitHub Desktop.
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()
@stevepeak
Copy link
Author

stevepeak commented Sep 25, 2016

Thank you community!

Demo GitHubs integration on Codecov! https://github.com/integration/codecov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment