0

我正在尝试通过 Apple Search Ads API 获取报告数据。所以我使用方法 -H ...\ -d "@TestSearchTermReport.json" -X POST "/v1/reports/campaigns/{cId}/searchterms"

我已包含所有标题和凭据。以下是我的 POST 正文的 json 数据文件的内容: { "startTime": "2016-11-13", "endTime": "2016-11-13", "timezone": "UTC", "granularity" ": "DAILY", "selector": { "orderBy":[{"field":"spend","sortOrder":"DESCENDING"}], "fields": ["spend", "taps", "conversions ", "avgCPA", "avgCPC", "ttr", "conversionRate"], "pagination": { "offset": 0, "limit": 1000 } }, "groupBy": "countryCode", "returnRowTotals":错误的, ”

但是,我收到以下错误消息:

{"data":null,"pagination":null,"error":{"errors":[{"messageCode":"INVALID_JSON_REQUEST","message":"这是一个无效的 json。请求无法解析" ,"字段":"行#:1 列#:3"}]}}

我通过不同的方式尝试了很多次,但仍然无法正常工作。有没有聪明的人可以帮助我?

提前致谢!

4

3 回答 3

6

我自己一直在为这个 API 苦苦挣扎,文档并不完全是用户友好的!

看起来你在这里有一些问题:

  • Timezone 和 Granularity 是枚举,因此它们的值必须是数字,而不是字符串。实际上,每次使用 timezone 字段调用时,我仍然会遇到错误,所以暂时省略了这个,直到我找到解决方案。
  • 您的某些字段名称不正确;send、avgCPC 和 countryCode 应分别为 localSpend、avgCPT 和 COUNTRY_CODE。
  • group by 字段应该是一个列表。

当你使用 python 时,试试这个:

import requests

org_id = <YOUR_ORG_ID>
certificate_path = '<PATH_TO_YOUR_CERTIFICATE>'
certificate_key_path = '<PATH_TO_YOUR_CERTIFICATE_KEY>'
campaign_id = <YOUR_CAMPAIGN_ID>


headers = {"Authorization": "orgId=%s" % org_id}    
payload = {
                "startTime": "2016-11-13", 
                "endTime": "2016-11-13",
                "granularity": 1, 
                "selector": {
                    "orderBy":[{"field":"localSpend","sortOrder":"DESCENDING"}], 
                    "fields": ["localSpend", "taps", "conversions", "avgCPA", "avgCPT", "ttr", "conversionRate"], 
                    "pagination": { "offset": 0, "limit": 1000 }
                    }, 
                "groupBy": ["COUNTRY_CODE"], 
                "returnRowTotals": False, 
                "returnRecordsWithNoMetrics": False
            }
url = 'https://api.searchads.apple.com/api/v1/reports/campaigns/%s/searchterms' % campaign_id
response = requests.post(url, cert=(certificate_path, certificate_key_path), json=payload, headers=headers)
print(response.text)

这将为我返回一个成功的响应。希望它也适合你!

于 2016-11-24T11:49:35.520 回答
1

我能够使用以下 curl 让它工作

curl --cert ./<PI2 CERTIFICATE FILE>.p12 --pass <PI2 CERTIFICATE PASSWORD>  -H "Authorization: orgId=xxx"  -H "Content-Type: application/json" -X POST -d ' {"startTime": "2017-04-06", "endTime": "2017-04-06", "granularity": 2, "selector": {"orderBy":[{"field":"localSpend","sortOrder":"DESCENDING"}], "fields": ["localSpend"], "pagination": { "offset": 0, "limit": 1000 } }, "groupBy": ["COUNTRY_CODE"], "returnRowTotals": false, "returnRecordsWithNoMetrics": false }' "https://api.searchads.apple.com/api/v1/reports/campaigns/campaign name/searchterms"

您可以按照此处提到的步骤获取 p12 证书 https://developer.apple.com/library/content/documentation/General/Conceptual/AppStoreSearchAdsAPIReference/API_Overview.html#//apple_ref/doc/uid/TP40017495-CH7- SW8

如果您在 python 中使用请求进行 post 调用,您可能需要做一些额外的工作,因为我没有找到任何将 p12 证书和密码作为输入的参数。使用 openssl 创建 crt 文件和 pem

openssl pkcs12 -in Apple_Certificate.p12 -out file.crt.pem -clcerts -nokeys
openssl pkcs12 -in Apple_Certificate.p12 -out file.key.pem -nocerts -nodes

并使用以下代码

headers = {
            'Authorization': 'orgId=<ORG_ID>',
            'Content-Type': 'application/json',
          }
data = ' {"startTime": "%s",
          "endTime": "%s", 
          "granularity": 2, ' \
          '"selector": {"orderBy":[{"field":"localSpend","sortOrder":"DESCENDING"}], ' \
          '"fields": ["localSpend"], "pagination": { "offset": 0, "limit": 1000 } }, ' \
          '"groupBy": ["COUNTRY_CODE"], "returnRowTotals": false, "returnRecordsWithNoMetrics": false }' % (date_report, date_report) 
url = 'https://api.searchads.apple.com/api/v1/reports/campaigns/%s/searchterms' % (your_campaign_id)
r = requests.post(url, headers=headers, data=data,
                                      cert=('<path to crt file>',
                                            '<path to key file>'))
于 2017-04-21T13:38:46.240 回答
0

您可以使用邮递员:

步骤: 1. 从 Postman 导入 Row 文本 -在此处输入图像描述

curl -X GET ' https://api.searchads.apple.com/api/v2/campaigns/124324 ' -H '授权:orgId=234234' -H 'Content-Type: application/json'

  1. 在 Postman 设置中设置证书和密钥(我的 macOS 配置) -在此处输入图像描述

    1. 现在您可以在邮递员处使用请求 -在此处输入图像描述
于 2019-09-23T13:43:38.170 回答