为什么亚马逊广告报告 API 返回 .bin 而不是 .json

Jar*_*ibb 3 amazonsellercentral amazon-advertising-api

我正在向亚马逊广告 API 请求一份赞助产品报告。当我发送 POST 时,我收到了 reportID。我输入 reportID 作为 GET 调用的路径的一部分来检索文档。我观察到类型 20 的响应,但是响应的内容是二进制代码(我认为)。

文档表明我应该收到 JSON 响应,但这不是我返回的内容。如何以适当的格式返回文档?

我附上了一张图片以供参考。

邮递员回应

mr_*_*cow 6

正如@tector所说,您需要解压缩该文件。亚马逊的文档相当糟糕,并且经常混有更新和过时的信息。

当状态为“SUCCESS”时,GET /v2/reports/{reportId} 确实会返回包含下载 URI 的 JSON 响应。

GET /v2/reports/{reportId}/download 成功后,将通过 307 重定向响应生成的文件。您看到的是二进制响应,因为它是一个文件。

在 python 中,你可以像这样处理响应:

import requests
import gzip
import io

headers = {
    "Authorization": f"Bearer {access_code}",
    "Amazon-Advertising-API-Scope": profile_id,
    "Amazon-Advertising-API-ClientId": client_id
}

response = requests.get(location, headers=headers, allow_redirects=True)
if response.ok:
    compressed_file = io.BytesIO(response.content)
    decompressed_file = gzip.GzipFile(fileobj=compressed_file)  # you can shorten the past 2 lines
    final = pd.read_json(decompressed_file)  # put contents into pandas dataframe
Run Code Online (Sandbox Code Playgroud)