首页 > 解决方案 > 为什么亚马逊广告报告 API 返回 .bin 而不是 .json

问题描述

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

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

我已经包含了一张图片以供参考。

邮递员回应

标签: amazonsellercentralamazon-advertising-api

解决方案


正如@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

推荐阅读