Teach you how to build Amazon advertising API development environment from scratch to obtain SP advertising data

Posted by fatmikey on Thu, 13 Jan 2022 20:26:00 +0100

1. Get access_token

Official link

1.1 request path POST

regionURL
NAhttps://api.amazon.com/auth/o2/token
EUhttps://api.amazon.co.uk/auth/o2/token
FEhttps://api.amazon.co.jp/auth/o2/token

1.2 request cases

curl \                                                                                                                                                            -X POST \    -H "Content-Type:application/x-www-form-urlencoded;charset=UTF-8" \    --data "grant_type=refresh_token&client_id=YOUR_CLIENT_ID&refresh_token=YOUR_REFRESH_TOKEN&client_secret=YOUR_CLIENT_SECRET" \    https://api.amazon.com/auth/o2/token

1.3 code practice

 //Get access_token method, taking NA region as an example.
HashMap<String, Object> map = new HashMap<>();
map.put("grant_type","refresh_token");            map.put("refresh_token","your refresh_token");
map.put("client_id","your client_id");
map.put("client_secret","your client_secret");
String getAccessUrl = "https://api.amazon.com/auth/o2/token";
String result = HttpUtil.doPost(getAccessUrl,map,null);
Map map1 = JSONObject.parseObject(result, Map.class);
String access_token = (String) map1.get("access_token");
System.out.println("access_token = " + access_token);

The operation results are as follows:

2. Get profileId

Official connection

2.1 request path GET

https://advertising-api.amazon.com/v2/profiles

2.2 request parameters

Parameter namePossible values (string)
apiProgrambilling, campaign, paymentMethod, store, report, account, posts
accessLeveledit, view
profileTypeFilterseller, vendor, agency
validPaymentMethodFiltertrue, false

Request header:

keyvalue
Content-Typeapplication/json
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id

2.3 code practice

String url = "https://advertising-api.amazon.com/v2/profiles?apiProgram=billing&profileTypeFilter=seller&validPaymentMethodFilter=true";
HashMap<String, String> headerMap = new HashMap<>();
headerMap.put("Content-Type","application/json");
headerMap.put("Authorization","Bearer "+access_token);
headerMap.put("Amazon-Advertising-API-ClientId","your client_id");
String result1 = HttpUtil.doGet1(url,headerMap);
List<Map> profileIds = JSONObject.parseArray(result1, Map.class);
System.out.println("profileIds = " + profileIds);

The operation results are as follows:

3. Create sp_campaign Report

Official link

⚠️: The official document's Responses are incorrect. Everyone understands them. The actual Responses have been given priority.

3.1 request path POST

https://advertising-api.amazon.com/v2/sp/campaigns/report

3.2 request parameters

Request body parameters:

keyValue
stateFilterenabled, paused, archived
campaignTypesponsoredProducts
segmentquery, placement
reportDateYYYYMMDD
metricsPass in the value you want to get

Request header:

keyvalue
Content-Typeapplication/json
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeProfileID (obtained in step 2)
Authorizationaccess_token

3.3 code practice

/**
*The second step is to obtain a List and select the qualified ones for operation. In this operation, type=seller, profileID = XXXXXXX, countryCode=CA are selected
*/
String createSpReport = "https://advertising-api.amazon.com/v2/sp/campaigns/report";
//Construct request header
HashMap<String, String> headerMap1 = new HashMap<>();
headerMap1.put("Content-Type","application/json");
headerMap1.put("Amazon-Advertising-API-ClientId","your client_id");
headerMap1.put("Amazon-Advertising-API-Scope",profileId.toString());
headerMap1.put("Authorization","Bearer "+access_token);
//Parameters of the request body
HashMap<String, Object> paramMap = new HashMap<>();
//paramMap.put("stateFilter", "enabled");
//paramMap.put("campaignType","sponsoredProducts");
//paramMap.put("segment","query");
paramMap.put("reportDate","20210701");
paramMap.put("metrics","campaignName,campaignId,impressions,clicks,cost,attributedConversions14d,attributedSales14d");
String s2 = HttpUtil.doPostBody(createSpReport, JSONObject.toJSONString(paramMap),headerMap1);   
System.out.println("s2 = " + s2);

The operation results are as follows:

4. Obtain the report download address

4.1 request path GET

https://advertising-api.amazon.com/v2/reports/{reportId}

4.2 request parameters

Request header:

keyvalue
Content-Typeapplication/json
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeprofileId

4.3 code practice

/**
	When status=SUCCESS, it means that the report has been created. At this time, go to get the download URL
*/
String getSpReport = "https://advertising-api.amazon.com/v2/reports/"+reportId;
HashMap<String, String> header = new HashMap<>();
header.put("Content-Type","application/json");
header.put("Authorization","Bearer "+access_token);
header.put("Amazon-Advertising-API-ClientId","your client_id");
header.put("Amazon-Advertising-API-Scope",profileId.toString());
String report = HttpUtil.doGet1(getSpReport, header);
reportMap = JSONObject.parseObject(report, Map.class);
String downUrl = reportMap.get("location").toString();

The operation results are as follows:

5. Download report 1

Official link

The official document only has the downloaded API in the sd advertisement

5.1 request path GET

downUrl obtained in step 4

5.2 request parameters

Request header parameters:

keyvalue
Content-Typeapplication/json
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeprofileId

5.3 code practice

/**
The downUrl obtained in step 4 is not the final download address, so you need to request it again.
*/
HashMap<String, String> headerMap2 = new HashMap<>();
headerMap2.put("Content-Type","application/json");
headerMap2.put("Authorization","Bearer "+access_token);
headerMap2.put("Amazon-Advertising-API-ClientId","your client_id");
headerMap2.put("Amazon-Advertising-API-Scope",profileId.toString());
CloseableHttpResponse response = HttpUtil.doGetReturnResponse(downUrl, headerMap2);
Header[] locations = response.getHeaders("Location");
System.out.println("locations = " + locations);

6. Download statement II

6.1 request path GET

The url obtained in step 5

6.2 request parameters

Request header parameters:

keyvalue
Accept-Encodinggzip
Acceptapplication/octet-stream

6.3 code practice

HashMap<String, String> header = new HashMap<>();
header.put("Accept-Encoding","gzip");
header.put("Accept","application/octet-stream");
String s3 = HttpUtil.doGet3("url", header);
System.out.println("s3 = " + s3);

The results are as follows:

7. Obtain the portfolio ID according to the campaign ID

Official documents

Since the information obtained in step 6 does not contain the portfolio ID, continue to obtain the portfolio ID.

7.1 request path GET

https://advertising-api.amazon.com/v2/sp/campaigns

7.2 request parameters

In this request, only the campaignIdFilter parameter is transmitted,

Request header parameters:

keyvalue
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeprofileId
Content-Typeapplication/json

7.3 code practice

/**
  campaignId_param Concatenate all campaign IDS with commas
*/
String getPortfolioId_url = "https://advertising-api.amazon.com/v2/sp/campaigns?campaignIdFilter="+campaignId_param;
HashMap<String, String> headerMap3 = new HashMap<>();
headerMap3.put("Authorization","Bearer "+access_token);
headerMap3.put("Amazon-Advertising-API-ClientId","your client_id");
headerMap3.put("Amazon-Advertising-API-Scope",profileId.toString());
headerMap3.put("Content-Type","application/json");
String s4 = HttpUtil.doGet1(getPortfolioId_url, headerMap3);
System.out.println("Acquired portfolioId = " + s4);

The execution results will not be demonstrated.

7.4 obtain portfolio information according to portfolio ID

Official documents

7.5 request path

https://advertising-api.amazon.com/v2/portfolios

7.6 request parameters

nametypedescribe
portfolioIdstringRetrieves the portfolio with the specified ID
portfolioNamestringRetrieves the portfolio with the specified name
portfolioStatestringRetrieves the portfolio with the specified status

Request header parameters:

keyvalue
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeprofileId
Content-Typeapplication/json

7.7 code practice

/**
	portfolioIdFilter The portfolio IDs are spliced together with commas, but the maximum number is 100 at a time.
*/
String getPortfolios_url = "https://advertising-api.amazon.com/v2/portfolios?portfolioIdFilter="+portfolioIdFilter;
HashMap<String, String> headerMap4 = new HashMap<>();
headerMap4.put("Authorization","Bearer "+access_token);
headerMap4.put("Amazon-Advertising-API-ClientId","your client_id");
headerMap4.put("Amazon-Advertising-API-Scope",profileId.toString());
headerMap4.put("Content-Type","application/json");
String s5 = HttpUtil.doGet1(getPortfolios_url, headerMap4);
System.out.println("Acquired portfolio = " + s5); 

The execution results will not be demonstrated.

The advertising data of this sp has been obtained, and the processing data can be saved to a file.

Topics: api Amazon