Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save cfa532/e960621803146e08fb6848bbd5bf3ee6 to your computer and use it in GitHub Desktop.

Select an option

Save cfa532/e960621803146e08fb6848bbd5bf3ee6 to your computer and use it in GitHub Desktop.
Connection to the App Store Connect API using Python3

How to connect to the App Store Connect API using Python3

You will need the requests and authlib packages. Just run :

$ pip install requests authlib

Then you need to generate an API Key from the App Store Connect portal (https://developer.apple.com/documentation/appstoreconnectapi/creating_api_keys_for_app_store_connect_api).

In the Python code below, replace the values of KEY_ID, ISSUER_ID and PATH_TO_KEY with your own.

You can change the URL of the request you want to make with the URL variable.

Finally, if you have some query parameters to pass in, use the params parameter of requests.get() (or requests.post()).

import requests, time, json
from authlib.jose import jwt
KEY_ID = "XXXXXXXXXX"
ISSUER_ID = "XXXXXX-XXXXXXX-XXXXXX-XXXXXXX"
EXPIRATION_TIME = int(round(time.time() + (20.0 * 60.0))) # 20 minutes timestamp
PATH_TO_KEY = 'path/to/your/key.p8'
with open(PATH_TO_KEY, 'r') as f:
PRIVATE_KEY = f.read()
header = {
"alg": "ES256",
"kid": KEY_ID,
"typ": "JWT"
}
payload = {
"iss": ISSUER_ID,
"exp": EXPIRATION_TIME,
"aud": "appstoreconnect-v1"
}
# Create the JWT
token = jwt.encode(header, payload, PRIVATE_KEY)
# API Request
JWT = 'Bearer ' + token.decode()
URL = 'https://api.appstoreconnect.apple.com/v1/users'
HEAD = {'Authorization': JWT}
r = requests.get(URL, params={'limit': 200}, headers=HEAD)
# Write the response in a pretty printed JSON file
with open('output.json', 'w') as out:
out.write(json.dumps(r.json(), indent=4))
@cfa532
Copy link
Author

cfa532 commented Jun 7, 2024

Thank you for posting the solution. It is a pain in the ass to figure it out in the labyrinth that Apple calls document.

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