Created
January 17, 2024 18:25
-
-
Save jameslmartin/0b5ba47ae0c47b8cb392e1ab0b839bcb to your computer and use it in GitHub Desktop.
GitHub App Debugging
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
import time | |
import sys | |
import requests | |
import pprint | |
from jwt import jwk_from_pem, JWT | |
pem = 'path/to/your/private_key.pem' | |
app_id = '<your app ID>' | |
# Open PEM | |
with open(pem, 'rb') as pem_file: | |
signing_key = jwk_from_pem(pem_file.read()) | |
payload = { | |
# Issued at time | |
'iat': int(time.time()), | |
# JWT expiration time (10 minutes maximum) | |
'exp': int(time.time()) + 600, | |
# GitHub App's identifier | |
'iss': app_id | |
} | |
# Create JWT | |
jwt_instance = JWT() | |
encoded_jwt = jwt_instance.encode(payload, signing_key, alg='RS256') | |
print(f"JWT: {encoded_jwt}") | |
headers = { | |
"Authorization": f"Bearer {encoded_jwt}", | |
"Accept": "application/vnd.github+json", | |
"X-GitHub-Api-Version": "2022-11-28", | |
"x-accepted-github-permissions": "" | |
} | |
# Get app installations (manual step done by an Admin) | |
app = requests.get("https://api.github.com/app/installations", headers=headers) | |
print("\nApp information") | |
pprint.pp(app.json()) | |
app_installation_id = app.json()[0].get("id") | |
# Get Access Token | |
r = requests.post(f"https://api.github.com/app/installations/{app_installation_id}/access_tokens", headers=headers) | |
access_token = r.json().get("token") | |
print("\n" + str(access_token)) | |
headers["Authorization"] = f"Bearer {access_token}" | |
print("\nUse GraphQL to get members in org") | |
# GraphQL request to get members in organization | |
query = """ | |
query users($org: String!, $cursor: String) { | |
organization(login: $org) { | |
membersWithRole(first: 100, after: $cursor) { | |
pageInfo { hasNextPage, endCursor } | |
nodes { | |
avatarUrl, | |
bio, | |
login, | |
name, | |
organizationVerifiedDomainEmails(login: $org) | |
} | |
} | |
} | |
} | |
""" | |
# query = """query { viewer { login }}""" | |
variables = { | |
"org": "<your-org>" | |
} | |
headers["Authorization"] = f"Bearer {access_token}" | |
r = requests.post("https://api.github.com/graphql", headers=headers, json={"query": query, "variables": variables}); | |
print(r.status_code) | |
print(r.json()) | |
pprint.pp(r.headers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment