Created
October 21, 2024 21:57
-
-
Save Sdy603/7be366e4021a430852905df7e78ea7b9 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
import os | |
import requests | |
import jwt | |
import time | |
# Load environment variables | |
app_id = os.getenv('GITHUB_APP_ID') | |
private_key_file = os.getenv('GITHUB_RSA_PRIVATE_KEY_PATH') | |
with open(private_key_file, 'r') as f: | |
private_key = f.read() | |
# Create JWT | |
def create_jwt(app_id, private_key): | |
payload = { | |
"iat": int(time.time()), | |
"exp": int(time.time()) + (10 * 60), # Token valid for 10 minutes | |
"iss": app_id | |
} | |
return jwt.encode(payload, private_key, algorithm="RS256") | |
jwt_token = create_jwt(app_id, private_key) | |
# List installations of the GitHub App | |
url = "https://api.github.com/app/installations" | |
headers = { | |
"Authorization": f"Bearer {jwt_token}", | |
"Accept": "application/vnd.github+json" | |
} | |
response = requests.get(url, headers=headers) | |
if response.status_code == 200: | |
print("Installations:", response.json()) | |
else: | |
print(f"Failed to list installations: {response.status_code}, {response.text}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment