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

Trying to Retrieve Report from Amazon Ads API

我正在尝试检索我生成的报告,并显示了 'successful' 的 'status'。

当我 运行 下面的脚本时,我收到了 200 响应。但是,根据文档:

“成功调用 returns 307 重定向响应。重定向 link 将您指向可以下载报告文件的 S3 存储桶。报告文件下载为 JSON以 gzip 格式压缩。"

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