Skip to content

Instantly share code, notes, and snippets.

@dbrent-amazon
Last active April 19, 2022 18:27
Show Gist options
  • Save dbrent-amazon/ca396a63c1670ee0ec83aad26b0ce55b to your computer and use it in GitHub Desktop.
Save dbrent-amazon/ca396a63c1670ee0ec83aad26b0ce55b 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
@garybunofsky
Copy link

garybunofsky commented Jun 13, 2018

Hey @dbrent-amazon. Thanks you for this gist. It's helped me quite a bit already. You seem to be pretty familiar with the Amazon Advertising API. I was hoping that you could help me, as I've searched high and low for clarification on my current issue but haven't had any luck.

My goal is to get current campaigns through the Amazon Advertising API so that I can generate reports. I've implemented LWA, and I'm having trouble getting profile data. I need to get profile data so I can pass the Amazon-Advertising-API-Scope in the header of subsequent requests.

When I make a GET request to https://advertising-api.amazon.com/v1/profiles I receive a response, which leads me to believe that my Authorization header is passed properly. However the response is an empty array. I would expect some type of profile to be in the array, as there are currently campaigns running under this account.

Do I have to create a profile for this account in order to view current campaigns? Is it possible to get the data of current campaigns with the Advertising API? Any help would be much appreciated

@Keita-N
Copy link

Keita-N commented Jan 16, 2019

I'm also dealing with the same trouble as @garybunofsky, Did you find the solution?

@dbrent-amazon
Copy link
Author

@garybunofsky and @Keita-N, empty profile list is a completely separate issue from reporting. You should open a Jira Service Desk ticket for that issue.

@ashfaque-byjus
Copy link

Hi!

After

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

I don't get a 307 redirect..I get a 401 HTTP 401 Unauthorized even though the actual token worked for previous requests i.e. to get
"reportId","status","statusDetails", "location","fileSize": 22

If I do the same request form postman, I get a 200 with junk data in body and not a 307 redirect

@m-junaid-butt
Copy link

Does Amazon Advertising provide web-hooks for report downloading? Actually, it takes time for some reports to be created so I have to request multiple times to get the download URL. Is there any way around for it? Kindly share your thoughts. Thanks

@alexbiship
Copy link

Hi there,

There's no any other way to download reports from advertising API. I understand your concern. The maximum report generation time is 15 mins and you should not make API requests synchronously which means you should use queue for report or snapshot functions.
image

@lucarigo
Copy link

lucarigo commented Mar 5, 2021

Hi, I'm having a problem at this point:

$ 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

When I make my request I get a response body that looks like this: "only one auth mechanism allowed; only the X-Amz-Algorithm query parameter…”
Does anyone know what is going on? Looks like the request is getting 2 authorization, but I'm only using the bearer Auth and the amazon-advertisign-API-scope

@dangquanghai
Copy link

Hi Everyone,
I would like to download report like this
curl -o /tmp/report.json.gz "https://-------------------b26ad5731/download"

How to setup curl -o in php. I am newbie
Please help me
thanks

@amistry007
Copy link

@lucarigo did you manage to resolve your issue with "only one auth mechanism allowed; only the X-Amz-Algorithm query parameter…” as I seem to be getting the same issue when trying to download the report.

@relstudiosnx
Copy link

Hi Everyone,

Is there any way to get the total spending and total sales of the campaign without generating a report? Reports usually take a lot of time to generate and process.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment