Skip to content

Instantly share code, notes, and snippets.

@corrieriluca
Last active April 23, 2024 04:50
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save corrieriluca/3c9f062401582dad83013d7363741b0c to your computer and use it in GitHub Desktop.
Save corrieriluca/3c9f062401582dad83013d7363741b0c 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment