#!/usr/bin/env python3
import jwt
import time
import requests

key_id = 'GA9C2GRPHS'
issuer_id = 'dcac5647-0710-4764-affd-2d3270bf49d4'

with open('AuthKey_GA9C2GRPHS.p8', 'r') as f:
    private_key = f.read()

token = jwt.encode(
    {'iss': issuer_id, 'exp': int(time.time()) + 1200, 'aud': 'appstoreconnect-v1'},
    private_key,
    algorithm='ES256',
    headers={'kid': key_id}
)

response = requests.get(
    'https://api.appstoreconnect.apple.com/v1/apps',
    headers={'Authorization': f'Bearer {token}'}
)

data = response.json()
print("Apps in App Store Connect:")
for app in data.get('data', []):
    print(f"  - {app['attributes']['name']}: {app['id']} (Bundle ID: {app['attributes']['bundleId']})")
