Apple Search Ads API Reporting POST Method Error

3.7k Views Asked by At

I'm trying to get report data through Apple Search Ads API. So I use the method -H ...\ -d "@TestSearchTermReport.json" -X POST "/v1/reports/campaigns/{cId}/searchterms"

I have included all the headers and credentials. The following is the content of my json data file for the POST body: { "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": False, "returnRecordsWithNoMetrics": False }

However, I get the following error message:

{"data":null,"pagination":null,"error":{"errors":[{"messageCode":"INVALID_JSON_REQUEST","message":"This is an invalid json. The request can not be parsed","field":"Line#:1 Column#:3"}]}}

I have tried many times through different ways, but still not working. Is there any smart guys can help me?

Thanks in advance!

3

There are 3 best solutions below

1
On

I've just been struggling with this API myself, the documentation is not exactly user friendly!

Looks like you have a few issues here:

  • Timezone and Granularity are enums, so their values need to be numeric, not strings. I'm actually still getting an error every time I call with the timezone field, so have omitted this for the time being, until I can find a solution.
  • Some of your field names are incorrect; spend, avgCPC and countryCode should be localSpend, avgCPT and COUNTRY_CODE, respectively.
  • The group by field should be a list.

As you're using python, try this:

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)

This returns a successful response for me. Hope it works for you too!

0
On

I was able to get it working using the following 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"

You can obtain the p12 certificate by following the steps mentioned here https://developer.apple.com/library/content/documentation/General/Conceptual/AppStoreSearchAdsAPIReference/API_Overview.html#//apple_ref/doc/uid/TP40017495-CH7-SW8

If you are using requests in python to make the post call you might have to do some extra work as I did not find any parameters that takes in p12 certificate and password as input. Create a crt file and pem using openssl

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

and use the following code

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>'))
0
On

You can use the postman:

Steps: 1. Import Row text from the Postman - enter image description here

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

  1. Set certificate and key in Postman settings (My configuration for macOS ) - enter image description here

    1. Now you can use the request at postman - enter image description here