【问题标题】:Why does Amazon Advertising report API return a .bin instead of .json为什么亚马逊广告报告 API 返回 .bin 而不是 .json
【发布时间】:2021-09-28 20:56:17
【问题描述】:
我正在向亚马逊的广告 API 请求赞助产品报告。当我发送 POST 时,我收到了 reportID。我输入 reportID 作为 GET 调用路径的一部分来检索文档。我观察到类型 20 的响应,但是响应的内容是二进制代码(我认为)。
文档表明我应该收到 JSON 响应,但这不是我要返回的。如何以适当的格式返回文档?
我已附上一张图片以供回复参考。
【问题讨论】:
标签:
amazonsellercentral
amazon-advertising-api
【解决方案1】:
正如@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
【解决方案2】:
是的,这很令人困惑。事实上,它是一个 gzipped 文件,您必须下载并解压缩: