Hi using information on Amazon App Submittion API, I write a python code to upload apk files to Amazon App Store. Although I tried to follow the instructions on the official reference, I could not managed to upload the apk. I always get Internal Server Error[Http 500]. Is there something wrong in my code or is it really issue of amazon servers? (p.s The part that is not working is replace_apk() function)
import sys
import requests
import json
import os
from pathlib import Path
def get_token():
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)
auth_response_json = auth_response.json()
access_token = auth_response_json["access_token"]
return access_token
BASE_URL = 'https://developer.amazon.com/api/appstore'
APP_ID = "XXXXXXXXXXXXXXXX"
CLIENT_ID = "XXXXXXXXXXXXXXXX"
CLIENT_SECRET = str(sys.argv\[1\])
ACCESS_TOKEN = get_token()
ACCESS_TOKEN_HEADER = {"Authorization": "Bearer %s" % ACCESS_TOKEN}
LOCAL_APK_PATH = str(sys.argv\[2\])
def get_edit():
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=ACCESS_TOKEN_HEADER)
current_edit = get_edits_response.json()
return current_edit
def create_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=ACCESS_TOKEN_HEADER)
new_edit = create_edit_response.json()
return new_edit
def replace_apk(edit_id):
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=ACCESS_TOKEN_HEADER).json()
firstAPK = apks[0]
apk_id = firstAPK['id']
get_specific_apk_url = BASE_URL + '/v1/applications/%s/edits/%s/apks/%s' % (APP_ID, edit_id, apk_id)
apk = requests.get(get_specific_apk_url, headers=ACCESS_TOKEN_HEADER)
etag = apk.headers['Etag']
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
local_apk = open(LOCAL_APK_PATH, 'rb').read()
apk_file_name = Path(LOCAL_APK_PATH).stem
all_headers = {
'Content-Type': 'application/vnd.android.package-archive',
'fileName': apk_file_name,
'If-Match': etag,
"Authorization": "Bearer " + ACCESS_TOKEN
}
replace_apk_response = requests.put(replace_apk_url, headers=all_headers, data=local_apk)
return replace_apk_response
if __name__ == '__main__':
current_edit = get_edit()
if not current_edit:
print("No open Edit")
current_edit = create_edit()
print("New Edit is created: ", current_edit)
else:
print("Edit exists")
if current_edit['status'] == "IN_PROGRESS":
upload_response = replace_apk(current_edit['id'])
print(upload_response)
else:
sys.exit("Unexpected app state. The app could be in review")
I have tried to upload the apk file to Amazon Appstore but getting 500 Error codes
EDIT: I added the successful version of replace_apk function which was getting 500 from amazon app store. I
def replace_apk(edit_id):
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=ACCESS_TOKEN_HEADER).json()
firstAPK = apks[0]
apk_id = firstAPK['id']
get_specific_apk_url = BASE_URL + '/v1/applications/%s/edits/%s/apks/%s' % (APP_ID, edit_id, apk_id)
apk = requests.get(get_specific_apk_url, headers=ACCESS_TOKEN_HEADER)
etag = apk.headers['Etag']
replace_apk_path = '/v1/applications/%s/edits/%s/apks/%s/replace' % (APP_ID, edit_id, apk_id)
local_apk = open(LOCAL_APK_PATH, 'rb').read()
_, apk_filename = os.path.split(LOCAL_APK_PATH)
replace_apk_url = BASE_URL + replace_apk_path
all_headers = {
'Content-Type': 'application/vnd.android.package-archive',
'If-Match': etag,
'fileName': apk_filename
}
all_headers.update(ACCESS_TOKEN_HEADER)
replace_apk_response = requests.put(replace_apk_url, headers=all_headers, data=local_apk)
return replace_apk_response