2

我正在沙盒环境中工作,我正在尝试根据https://advertising.amazon.com/API/docs/v2/reference/reports下载报告 问题是下载的报告是空的。里面不包含任何数据。我们如何从亚马逊广告 api 下载报告?

我按照以下步骤操作:https ://gist.github.com/dbrent-amazon/ca396a63c1670ee0ec83aad26b0ce55b

4

1 回答 1

5

这是一个适用于我使用 python 和请求的脚本,请确保:

  1. 创建广告系列、adGroupd 和关键字
  2. 创建具有正确指标的报告
  3. 确保您在报告中有要查看的实际数据
  4. 确保 reportDate 正确
import requests

version = 'v2'
advertise = 'sp'

headers = {
    "Authorization": f"Bearer {token.access}",
    "Amazon-Advertising-API-ClientId": AmazonSecurityProfile.ClientID,
    "Content-Type": "application/json",
}

class urls:
    class api:
        test = 'https://advertising-api-test.amazon.com'

# create report
recordType = "keywords"
r = requests.post(
    f'{urls.api.test}/{version}/{advertise}/{recordType}/report',
    json={
      # "campaignType": "sponsoredProducts",
      "segment": "query",
      "reportDate": '20201025',  #YYYYMMDD
      "metrics": ",".join([
          "campaignName",
          "campaignId",
          "campaignStatus",
          "campaignBudget",
          "clicks",
          "cost",
          "attributedConversions1d",
          "attributedConversions7d",
          "attributedConversions1dSameSKU",
          "attributedConversions7dSameSKU",
          "attributedUnitsOrdered1d",
          "attributedUnitsOrdered7d",
          "attributedSales1d",
          "attributedSales7d",
          "attributedSales1dSameSKU",
          "attributedSales7dSameSKU",
          "attributedUnitsOrdered1dSameSKU",
          "attributedUnitsOrdered7dSameSKU",
          "adGroupName",
          "adGroupId",
          "keywordText",
          "keywordId",
          "matchType",
          "impressions",
      ]),
    },
    headers=headers,
)
r.raise_for_status()
r = r.json()
print(r)
reportId = r["reportId"]

while r['status'] == 'IN_PROGRESS':
    r = requests.get(
        f'{urls.api.test}/{version}/reports/{reportId}',
        headers=headers,
    )
    r = r.json()
    print(r)

assert r['status'] == 'SUCCESS'

r = requests.get(
    r["location"],
    headers=headers,
)
print(r)
于 2020-10-25T19:33:54.163 回答