尝试从亚马逊广告API检索报告



我正在尝试检索我生成的报告,该报告显示"状态"为"成功"。

当我运行下面的脚本时,我会收到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]
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

最新更新