Skip to content

Instantly share code, notes, and snippets.

@JesusM
Created April 24, 2022 20:24
Show Gist options
  • Save JesusM/a38c183f88f7b8ebbf98bb3926a5dc06 to your computer and use it in GitHub Desktop.
Save JesusM/a38c183f88f7b8ebbf98bb3926a5dc06 to your computer and use it in GitHub Desktop.
Python script to submit apps to Amazon AppStore
import requests
import sys
from pathlib import Path
# Arguments you need to provide:
# 1.- client_id = Amazon client API id
# 2.- client_secret = Amazon client API secret
# 3.- app_id = Amazon app id
# 4.- local_apk_path = Path for the apk file to submit
arguments = sys.argv
client_id = arguments[1]
client_secret = arguments[2]
app_id = arguments[3]
local_apk_path = arguments[4]
print("---Arguments: " + str(arguments[1: 5: 1]) + "---\n")
BASE_URL = 'https://developer.amazon.com/api/appstore'
print("---Submitting new apk to Amazon Appstore for app id: " + app_id + "---\n")
print("--Getting required authentication--")
# Get authentication
scope = "appstore::apps:readwrite"
grant_type = "client_credentials"
data = {
"grant_type": grant_type,
"client_id": client_id,
"client_secret": client_secret,
"scope": scope
}
amazon_auth_url = "https://api.amazon.com/auth/o2/token"
auth_response = requests.post(amazon_auth_url, data=data)
# Read token from auth response
auth_response_json = auth_response.json()
auth_token = auth_response_json["access_token"]
auth_token_header_value = "Bearer %s" % auth_token
auth_token_header = {"Authorization": auth_token_header_value}
# Create edit
print("\n--Creating/grabbing new edit--")
create_edit_path = '/v1/applications/%s/edits' % app_id
create_edit_url = BASE_URL + create_edit_path
create_edit_response = requests.post(create_edit_url, headers=auth_token_header)
create_edit_code = create_edit_response.status_code
edit_etag = ""
if create_edit_code != 200:
print("Error creating new edit: " + create_edit_response.json()['message'] + ", getting current edit instead.")
get_edits_path = '/v1/applications/%s/edits' % app_id
get_edits_url = BASE_URL + get_edits_path
get_edits_response = requests.get(get_edits_url, headers=auth_token_header)
edit_etag = get_edits_response.headers['ETag']
edit_id = get_edits_response.json()['id']
print("Already available edit id:" + edit_id)
else:
edit_etag = create_edit_response.headers['ETag']
edit_id = create_edit_response.json()['id']
print("Edit created correctly: " + edit_id)
# Get the current list of APKs
print("\n--Getting apks list information--")
get_apks_path = '/v1/applications/%s/edits/%s/apks' % (app_id, edit_id)
get_apks_url = BASE_URL + get_apks_path
apks = requests.get(get_apks_url, headers=auth_token_header)
firstApk = apks.json()[0]
# Get apk information
apk_id = firstApk['id']
print("\n--Getting " + apk_id + " apk information--")
apkVersionCode = firstApk['versionCode']
get_apk_info_path = '/v1/applications/%s/edits/%s/apks/%s' % (app_id, edit_id, apk_id)
get_apk_info_url = BASE_URL + get_apk_info_path
apk_info = requests.get(get_apk_info_url, headers=auth_token_header)
apk_etag = apk_info.headers['Etag']
print("Apk id:" + apk_id + ", Version code:" + str(apkVersionCode) + ", Etag: " + apk_etag)
# Open the apk file on your local machine
print("\n--Uploading apk from local path: " + local_apk_path + "--")
local_apk_file = open(local_apk_path, 'rb')
apk_file_name = Path(local_apk_path).stem
local_apk = local_apk_file.read()
replace_apk_path = '/v1/applications/%s/edits/%s/apks/%s/replace' % (app_id, edit_id, apk_id)
replace_apk_url = BASE_URL + replace_apk_path
replace_apk_headers = {
'Content-Type': 'application/vnd.android.package-archive',
'If-Match': apk_etag,
'fileName': apk_file_name
}
replace_apk_headers.update(auth_token_header)
replace_apk_response = requests.put(replace_apk_url, headers=replace_apk_headers, data=local_apk)
if replace_apk_response.status_code != 200:
print("Apk replacement failed: " + str(replace_apk_response.json()['errors']))
else:
print("Apk replacement success")
# Validate edit
print("\n--Validating edit: " + edit_id + "--")
validate_edit_path = '/v1/applications/%s/edits/%s/validate' % (app_id, edit_id)
validate_edit_url = BASE_URL + validate_edit_path
validate_edit_response = requests.post(validate_edit_url, headers=auth_token_header)
response_json = validate_edit_response.json()
if response_json['httpCode'] != 200:
print("In order to submit this update, you need to manually visit the app submission page and address: " + str(
response_json['errors'][0]['errorMessage']))
else:
print("Update submitted successfully!")
@halysongoncalves
Copy link

Thank you so much for sharing this gist

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