1. Obtain access_token
The official link
1.1 Requesting Path POST
region | URL |
---|---|
NA | Api.amazon.com/auth/o2/tok… |
EU | API. Amazon. Co. UK/auth/o2 / tok… |
FE | API. Amazon. Co. jp/auth/o2 / tok… |
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” \ Api.amazon.com/auth/o2/tok…
1.3 Code practice
// Obtain the access_token. The NA area is used 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);
Copy the code
The running results are as follows:
2. Obtain profileId
The official connection
2.1 Request Path GET
https://advertising-api.amazon.com/v2/profiles
Copy the code
2.2 Request Parameters
The parameter name | Possible values (string) |
---|---|
apiProgram | billing, campaign, paymentMethod, store, report, account, posts |
accessLevel | edit, view |
profileTypeFilter | seller, vendor, agency |
validPaymentMethodFilter | true, false |
Request header:
key | value |
---|---|
Content-Type | application/json |
Authorization | access_token |
Amazon-Advertising-API-ClientId | your client_id |
2.3 Code practice
String url = "https://advertising-api.amazon.com/v2/profiles?apiProgram=billing&profileTypeFilter=seller&validPaymentMethodFilter=tru e";
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);
Copy the code
The running results are as follows:
3. Create the SP_Campaign report
The official link
⚠️ : There were errors in Responses in the official document, which were well understood and dominated by actual Responses.
3.1 Requesting path POST
https://advertising-api.amazon.com/v2/sp/campaigns/report
Copy the code
3.2 Request Parameters
Request body parameters:
key | Value | |
---|---|---|
stateFilter | enabled, paused, archived | |
campaignType | sponsoredProducts | |
segment | query, placement | |
reportDate | YYYYMMDD | |
metrics | Pass in the value you want to fetch |
Request header:
key | value |
---|---|
Content-Type | application/json |
Amazon-Advertising-API-ClientId | your client_id |
Amazon-Advertising-API-Scope | ProfileId (obtained in step 2) |
Authorization | access_token |
3.3 Code practice
Type =seller, profileId= XXXXXXXX, countryCode=CA */
String createSpReport = "https://advertising-api.amazon.com/v2/sp/campaigns/report";
// Construct the 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);
Copy the code
The running results are as follows:
4. Obtain the download address of the form
4.1 Requesting path GET
https://advertising-api.amazon.com/v2/reports/{reportId}
Copy the code
4.2 Request Parameters
Request header:
key | value |
---|---|
Content-Type | application/json |
Authorization | access_token |
Amazon-Advertising-API-ClientId | your client_id |
Amazon-Advertising-API-Scope | profileId |
4.3 Code practice
/** If status=SUCCESS, the report has been created
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();
Copy the code
The running results are as follows:
5. Download Report 1
The official link
Official documents only have an API for downloading in SD ads
5.1 Requesting a Path GET
DownUrl obtained in Step 4
5.2 Request Parameters
Request header parameters:
key | value |
---|---|
Content-Type | application/json |
Authorization | access_token |
Amazon-Advertising-API-ClientId | your client_id |
Amazon-Advertising-API-Scope | profileId |
5.3 Code practice
/** The downUrl obtained in Step 4 is not the final download address, 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);
Copy the code
6. Download Report 2
6.1 Requesting a Path GET
Url obtained in Step 5
6.2 Request Parameters
Request header parameters:
key | value |
---|---|
Accept-Encoding | gzip |
Accept | application/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);
Copy the code
The result is as follows:
7. Get portfolioId according to campaignId
The official documentation
Because the information obtained in Step 6 does not contain portfolioids, continue to get portfolioids.
7.1 Requesting path GET
https://advertising-api.amazon.com/v2/sp/campaigns
Copy the code
7.2 Request Parameters
This request only transmits the campaignIdFilter parameter,
Request header parameters:
key | value |
---|---|
Authorization | access_token |
Amazon-Advertising-API-ClientId | your client_id |
Amazon-Advertising-API-Scope | profileId |
Content-Type | application/json |
7.3 Code practice
/** campaignId_param stitched together with commas for all campaignids */
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("Obtained portfolioId =" + s4);
Copy the code
The execution results are not demonstrated.
7.4 Obtaining portfolio information by portfolioId
The official documentation
7.5 Request Path
https://advertising-api.amazon.com/v2/portfolios
Copy the code
7.6 Request Parameters
name | type | describe |
---|---|---|
portfolioId | string | Retrieves the portfolio with the specified ID |
portfolioName | string | Retrieves the portfolio with the specified name |
portfolioState | string | Retrieves the portfolio with the specified state |
Request header parameters:
key | value |
---|---|
Authorization | access_token |
Amazon-Advertising-API-ClientId | your client_id |
Amazon-Advertising-API-Scope | profileId |
Content-Type | application/json |
7.7 Code practice
/** portfolioIdFilter portfolioids are joined together by commas, but up to 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("Obtained portfolio =" + s5);
Copy the code
The execution results are not demonstrated.
To this SP advertising data has been obtained, processing data can be saved to the file.
PS: If you think it is helpful to you, you can leave a message in three consecutive times. Welcome to put forward your valuable opinions. If you want to have technical exchanges, you are welcome to join the Amazon API development Communication Group! Please add technical personnel on wechat:x118422Invite into the group