Skip to content

Instantly share code, notes, and snippets.

@zzl0
Forked from dbrent-amazon/your-first-report
Created December 18, 2020 19:41
Show Gist options
  • Save zzl0/d32d4052e276697151feeab82ae7e79d to your computer and use it in GitHub Desktop.
Save zzl0/d32d4052e276697151feeab82ae7e79d to your computer and use it in GitHub Desktop.
Note that this is sandbox. If you have not registered profiles in sandbox you
will need to do so prior to making these calls. You will also need at least
one campaign, ad group, and keyword before you see anything in the report.
While in sandbox, we will return “dummy” data so that you can see how an actual
report would look.
To make it look nicer, I always export my access token prior to making calls.
You could do the same for API-Scope (profile Id) if you wish. Make sure to use
quotes around the access token if it isn’t URL encoded. It has a | (pipe)
symbol and your shell won’t like it.
$ export AUTH="MY_ACCESS_TOKEN"
1) Request the report:
$ curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: bearer $AUTH" \
-H "Amazon-Advertising-API-Scope: ABC1234567890" \
-d '{"reportDate": "20161010", "campaignType": "sponsoredProducts", "metrics": "impressions,clicks,cost", "segment": "query"}' \
https://advertising-api-test.amazon.com/v1/keywords/report
Response:
{
"reportId":"amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731",
"recordType":"keyword",
"status":"IN_PROGRESS",
"statusDetails":"Report is being generated."
}
2) Get the report (actually getting the location for downloading):
$ curl \
-H "Authorization: bearer $AUTH" \
-H "Amazon-Advertising-API-Scope: ABC1234567890" \
https://advertising-api-test.amazon.com/v1/reports/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731
Response:
{
"reportId":"amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731",
"status":"SUCCESS",
"statusDetails":"Report has been generated successfully. Pending publish to S3.",
"location":"https://advertising-api-test.amazon.com/v1/reports/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731/download",
"fileSize":236
}
3) Get the download location to S3. This is returned in the header. The file
is also gzipped. Notice the use of the -v flag so CURL will show headers.
$ curl -v \
-H "Authorization: bearer $AUTH" \
-H "Amazon-Advertising-API-Scope: ABC1234567890" \
https://advertising-api-test.amazon.com/v1/reports/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731/download
Response:
< HTTP/1.1 307 Temporary Redirect
< Date: Fri, 14 Oct 2016 21:17:49 GMT
< Server: Apache-Coyote/1.1
< Location: https://sandboxreports.s3.amazonaws.com/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731?AWSAccessKeyId=AKIAIKLHNT32USZOWVRA&Expires=1476479900&Signature=I%2F2Gd%2B8TbcPbXbBUM6ix%2BSVP3qA%3D
< x-amz-request-id: 067KMA80XJHXHQ1P37SA
< Content-Length: 0
< Vary: Accept-Encoding,User-Agent
4) “Location” is the S3 location where the report can be downloaded. You
don’t have to send authorization headers to get this. You can get it just
by visiting the URL. It will expire very quickly so be sure you get it ASAP.
You will probably need to put quotes around it as the URL contains characters
such as & that the shell will interpret as you trying to background the process,
which we don’t want.
$ curl -o /tmp/report.json.gz "https://sandboxreports.s3.amazonaws.com/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731?AWSAccessKeyId=AKIAIKLHNT32USZOWVRA&Expires=1476479900&Signature=I%2F2Gd%2B8TbcPbXbBUM6ix%2BSVP3qA%3D"
5) Profit:
$ less /tmp/report.json.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment