Skip to content

Instantly share code, notes, and snippets.

@Clivern
Last active April 27, 2021 12:07
Show Gist options
  • Save Clivern/621ed07d1e62451e659fffed9e013f02 to your computer and use it in GitHub Desktop.
Save Clivern/621ed07d1e62451e659fffed9e013f02 to your computer and use it in GitHub Desktop.
Authenticating with GitHub Apps

https://docs.github.com/en/developers/apps/authenticating-with-github-apps

pip install pyjwt
pip install cryptography
pip install pycryptodome

Create the token from installation ID

import jwt
import calendar
import time

secret_key = """
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAA.......
-----END RSA PRIVATE KEY-----
"""

app_id = 18984

token = jwt.encode({
    'iat': calendar.timegm(time.gmtime()) - 60,
    'exp': calendar.timegm(time.gmtime()) + 600,
    'iss': app_id
}, secret_key, algorithm='RS256').decode('utf-8')

print(token)

print("""
curl -i -X POST \
-H "Authorization: Bearer {}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations/:installation_id/access_tokens
""".format(token))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment