我正在嘗試檢索我生成的報告,并顯示“成功”的“狀態”。
當我運行下面的腳本時,我收到 200 回應。但是,根據檔案:
“成功的呼叫會回傳 307 重定向回應。重定向鏈接會將您指向 S3 存盤桶,您可以在其中下載報告檔案。報告檔案以 gzip 格式壓縮的 JSON 格式下載。”
headers = {
'Amazon-Advertising-API-ClientId': clientid,
'Authorization': access,
'Amazon-Advertising-API-Scope':scope,
'Content-Type':'application/json'
}
response = requests.get(
'https://advertising-api.amazon.com/v1/reports/amzn1.clicksAPI.v1.p1.624466CD.4b8dcbb2-bbc6-4936-a760-c632216a4a5e/download',
headers = headers
)
print(response.json)
回復:
<bound method Response.json of <Response [200]
uj5u.com熱心網友回復:
import gzip # unzip compressed file
import io # read binary
import requests
import pandas as pd
headers = {
"Authorization": f"Bearer {access_code}",
"Amazon-Advertising-API-Scope": profile_id,
"Amazon-Advertising-API-ClientId": client_id
}
response = requests.get(report_url, headers=headers)
if response.ok:
json_resp = response.json()
status = json_resp['status']
if status == 'IN_PROGRESS':
# check again
elif status == 'SUCCESS':
# initiate download
location = json_resp['location']
dl_response = requests.get(location, headers=headers, allow_redirects=True)
if dl_response.ok:
compressed_file = io.BytesIO(response.content) # extract .gz file
decompressed_file = gzip.GzipFile(fileobj=compressed_file) # unzip .gz file
output = pd.read_json(decompressed_file)
elif status == 'FAILURE':
# failure response
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/453155.html
標籤:Python python-3.x 亚马逊广告 API
