amazonsellercentral - 为什么亚马逊广告报告 API 返回 .bin 而不是 .json
问题描述
我正在向亚马逊的广告 API 请求赞助产品报告。当我发送 POST 时,我收到了 reportID。我输入 reportID 作为 GET 调用路径的一部分来检索文档。我观察到类型 20 的响应,但是响应的内容是二进制代码(我认为)。
文档表明我应该收到 JSON 响应,但这不是我要返回的。如何以适当的格式返回文件?
我已经包含了一张图片以供参考。
解决方案
正如@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
推荐阅读
- python - 如何安全地创建变量名的 SQLite3 数据库
- google-cloud-sql - Cloud SQL 在重启时卡住
- mysql - 插入元组产生错误:格式字符串的参数不足
- html - 为什么我的主容器太宽?只是一个 col-9 和 col-3
- javascript - 在 Angular 中从 JSON 生成 HTML
- c++ - 如何使用 C++ 使这个金字塔看起来像我的示例?
- php - 如何在mysqli准备语句中插入pourcentage符号%?
- api - Bigcommerce:编辑现有订单发货地址
- nginx - nginx 使用代理传递将所有尾随 / 重写为 /index.html
- r - R edgar 包 - geBusinDescr 的问题