Last active
February 25, 2019 12:19
-
-
Save hslatman/c85370d05ca48c3bcca8980751b11f79 to your computer and use it in GitHub Desktop.
Create App Store Connect API Token
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
from __future__ import print_function | |
import argparse | |
import os | |
import sys | |
import time | |
import jwt | |
def configure_with(args): | |
iss = args.iss | |
if not iss: | |
issuer_id_env_var = 'APP_STORE_CONNECT_ISSUER_ID' | |
try: | |
iss = os.environ[issuer_id_env_var] | |
except: | |
print("Please specify the {}".format(issuer_id_env_var)) | |
kid = args.kid | |
if not kid: | |
key_id_env_var = 'APP_STORE_CONNECT_KEY_ID' | |
try: | |
kid = os.environ[key_id_env_var] | |
except: | |
print("Please specify the {}".format(key_id_env_var)) | |
private_key_file = args.key_file | |
if not private_key_file: | |
potential_private_key_file = None | |
key_file_env_var = 'APP_STORE_CONNECT_PRIVATE_KEY_FILE' | |
try: | |
potential_private_key_file = os.environ[key_file_env_var] | |
except: | |
print("Please specify the {}".format(key_file_env_var)) | |
if potential_private_key_file and os.path.exists(potential_private_key_file): | |
private_key_file = open(potential_private_key_file, 'r') | |
else: | |
print("The provided private key file does not exist or can't be read!") | |
private_key = None | |
if private_key_file and os.path.isfile(private_key_file.name): | |
private_key = private_key_file.read() | |
return (iss, kid, private_key) | |
def get_token_with(args): | |
iss, kid, private_key = configure_with(args) | |
if iss is None or kid is None or private_key is None: | |
print("Please use the command line arguments or environment variables to set the requested parameter(s)!") | |
sys.exit(1) | |
headers = {'alg': 'ES256', 'kid': kid, 'typ': 'JWT'} | |
payload = {'iss': iss, 'exp': int(time.time() + 1150), 'aud': 'appstoreconnect-v1' } | |
encoded = jwt.encode(payload, private_key, algorithm='ES256', headers=headers) | |
return encoded | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description="Retrieve an App Store Connect API Token") | |
parser.add_argument('iss', nargs='?', type=str, help="The App Store Connect Issuer ID") | |
parser.add_argument('kid', nargs='?', type=str, help="The App Store Connect Key Id") | |
parser.add_argument('key_file', nargs='?', type=argparse.FileType('r'), help="(Path to) The App Store Connect Private Key") | |
args = parser.parse_args() | |
token = get_token_with(args) | |
print("\nTOKEN:\n\n{}\n".format(token)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment