开发人员控制台

Python示例


Python示例

以下示例显示了使用应用提交API执行各种任务时的Python代码。

获取访问令牌

使用客户端ID和客户端密钥获取身份验证令牌。您需要将身份验证令牌添加到每个API请求的标头。以下Python示例说明了如何获取身份验证令牌,并使用令牌创建Authorization标头。

# 需要提供的值
client_id = "<客户端ID>"
client_secret = "<客户端密钥>"
app_id = "<应用ID>"

BASE_URL = 'https://developer.amazon.com/api/appstore'

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()
auth_token = auth_response_json["access_token"]

auth_token_header_value = "Bearer %s" % auth_token

auth_token_header = {"Authorization": auth_token_header_value}

创建新Edit

以下Python示例说明了如何创建新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=headers)
current_edit = create_edit_response.json()

edit_id = current_edit['id']

获取打开的Edit

以下示例说明了如何获取已打开Edit的ID。

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=headers)
current_edit = get_edits_response.json()

edit_id = current_edit['id']

替换现有APK

以下示例说明了如何用本地计算机中的文件替换第一个APK文件。

## 获取当前的APK列表
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=headers)

firstAPK = apks[0]
apk_id = firstAPK['id']
replace_apk_path = '/v1/applications/%s/edits/%s/apks/%s/replace' % (app_id, edit_id, apk_id)

## 在本地计算机上打开APK文件
local_apk = open(local_apk_path, 'rb').read()

replace_apk_url = BASE_URL + replace_apk_path
all_headers = {
    'Content-Type': 'application/vnd.android.package-archive',
    'If-Match': etag
}
all_headers.update(headers)
replace_apk_response = requests.put(replace_apk_url, headers=all_headers, data=local_apk)

添加新APK

以下示例说明了如何上传新APK文件。

add_apk_path = '/v1/applications/%s/edits/%s/apks/upload' % (app_id, edit_id)
add_apk_url = BASE_URL + add_apk_path
local_apk = open(apk_location, 'rb').read()
all_headers = {
    'Content-Type': 'application/vnd.android.package-archive'
}
all_headers.update(headers)
add_apk_response = requests.post(add_apk_url, headers=all_headers, data=local_apk)
response = add_apk_response.json()

更新产品信息

以下示例说明了如何更新某个地区的应用商店产品信息。

listing_headers = headers.copy()
listing_headers.update({
    'Content-Type': 'application/json'
})
listings_etag, current_listing_json = get_current_listing(app_id, edit_id, language, listing_headers)

edit_body = current_listing_json.copy()
edit_body.update(edit_listing_body)
edit_listing_headers = listing_headers.copy()
edit_listing_headers.update({
    'If-Match': listings_etag
})

edit_listing_response = update_listing(app_id, edit_id, language, edit_body, edit_listing_headers)