Last active
January 5, 2023 08:16
-
-
Save nnsnodnb/3274616d99b20120eb04908588b092bf to your computer and use it in GitHub Desktop.
AppStoreConnect API sample
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
# pip install pyjwt requests cryptography | |
# and put "app-store-connect-api-key.json" in this script directory | |
import json | |
import time | |
from datetime import datetime, timedelta | |
from pathlib import Path | |
import jwt | |
import requests | |
app_store_connect_api_key_path = Path(".") / "app-store-connect-api-key.json" | |
class AppStoreConnectAPIKey(object): | |
def __init__(self, key_id, issuer_id, key, **kwargs): | |
self.key_id = key_id | |
self.issuer_id = issuer_id | |
self.key = key | |
json_object = json.loads(app_store_connect_api_key_path.read_text()) | |
api_key = AppStoreConnectAPIKey(**json_object) | |
now = datetime.now() | |
exp = now + timedelta(minutes=5) | |
token = jwt.encode( | |
{ | |
"iss": api_key.issuer_id, | |
"iat": int(time.mktime(now.timetuple())), | |
"exp": int(time.mktime(exp.timetuple())), | |
"aud": "appstoreconnect-v1", | |
}, | |
api_key.key, | |
algorithm="ES256", | |
headers={ | |
"alg": "ES256", | |
"kid": api_key.key_id, | |
"typ": "JWT" | |
} | |
) | |
url = "https://api.appstoreconnect.apple.com/v1/bundleIds" | |
headers = { | |
"Authorization": f"Bearer {token}", | |
} | |
res = requests.get(url, headers=headers) | |
if res.status_code == 401: | |
print("無効化済み") | |
elif res.status_code == 200: | |
print("有効だよ") | |
else: | |
print(f"不明なステータス: {res.status}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment