手把手教你從零開始搭建Amazon Advertising-API開發(fā)環(huán)境(二)之獲取SP廣告數(shù)據(jù)

1. 獲取access_token

官方鏈接

1.1 請求路徑 POST

地區(qū) URL
NA https://api.amazon.com/auth/o2/token
EU https://api.amazon.co.uk/auth/o2/token
FE https://api.amazon.co.jp/auth/o2/token

1.2 請求事例

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 代碼實(shí)操

 //獲取access_token的方法,以NA地區(qū)為例粟关。
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);

運(yùn)行結(jié)果如下:

image-20210726111027125.png

2. 獲取profileId

官方連接

2.1 請求路徑 GET

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

2.2 請求參數(shù)

參數(shù)名稱 可能的值(string)
apiProgram billing, campaign, paymentMethod, store, report, account, posts
accessLevel edit, view
profileTypeFilter seller, vendor, agency
validPaymentMethodFilter true, false

請求頭:

key value
Content-Type application/json
Authorization access_token
Amazon-Advertising-API-ClientId your client_id

2.3 代碼實(shí)操

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);

運(yùn)行結(jié)果如下:

image-20210726114127641.png

3. 創(chuàng)建sp_campaign報(bào)表

官方鏈接

??:此次官方文檔的Responses有誤仑鸥,大家懂的都懂粱檀,已實(shí)際的Responses為主短蜕。

3.1 請求路徑 POST

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

3.2 請求參數(shù)

請求體參數(shù):

key Value
stateFilter enabled, paused, archived
campaignType sponsoredProducts
segment query, placement
reportDate YYYYMMDD
metrics 傳入你想獲取的值

請求頭:

key value
Content-Type application/json
Amazon-Advertising-API-ClientId your client_id
Amazon-Advertising-API-Scope profileId(第二步獲取的)
Authorization access_token

3.3 代碼實(shí)操

/**
*第二步獲取的是個(gè)List,選擇符合條件的進(jìn)行操作膀斋,本次實(shí)操選擇的是type=seller,profileId=xxxxxxxx,countryCode=CA
*/
String createSpReport = "https://advertising-api.amazon.com/v2/sp/campaigns/report";
//構(gòu)造請求頭
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);
//請求體的參數(shù)
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);

運(yùn)行結(jié)果如下:

image-20210726135448721.png

4.獲取表報(bào)下載地址

4.1 請求路徑 GET

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

4.2 請求參數(shù)

請求頭:

key value
Content-Type application/json
Authorization access_token
Amazon-Advertising-API-ClientId your client_id
Amazon-Advertising-API-Scope profileId

4.3 代碼實(shí)操

/**
    當(dāng)status=SUCCESS的時(shí)候說明報(bào)表創(chuàng)建好了勃蜘,這時(shí)去獲取下載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();

運(yùn)行結(jié)果如下:

image-20210726142518285.png

5. 下載報(bào)表一

官方鏈接

官方文檔只有在sd廣告中才有下載的API

5.1 請求路徑 GET

步驟4中獲取的downUrl

5.2 請求參數(shù)

請求頭參數(shù):

key value
Content-Type application/json
Authorization access_token
Amazon-Advertising-API-ClientId your client_id
Amazon-Advertising-API-Scope profileId

5.3 代碼實(shí)操

/**
步驟4中獲取downUrl并不是最終的下載地址,還需再一次請求獲取夸政。
*/
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.下載報(bào)表二

6.1 請求路徑 GET

步驟5中獲取的url

6.2 請求參數(shù)

請求頭參數(shù):

key value
Accept-Encoding gzip
Accept application/octet-stream

6.3 代碼實(shí)操

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);

執(zhí)行結(jié)果如下:

image-20210726144600516.png

7.根據(jù)campaignId獲取portfolioId

官方文檔

由于步驟六中獲取的信息里不包含portfolioId元旬,所以繼續(xù)獲取portfolioId。

7.1 請求路徑 GET

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

7.2 請求參數(shù)

本次請求只傳campaignIdFilter參數(shù)守问,

請求頭參數(shù):

key value
Authorization access_token
Amazon-Advertising-API-ClientId your client_id
Amazon-Advertising-API-Scope profileId
Content-Type application/json

7.3 代碼實(shí)操

/**
  campaignId_param為所有的campaignId以逗號拼接在一起
*/
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("獲取的portfolioId = " + s4);

執(zhí)行結(jié)果就不演示了匀归。

7.4 根據(jù)portfolioId去獲取portfolio信息

官方文檔

7.5 請求路徑

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

7.6 請求參數(shù)

name type 描述
portfolioId string 檢索具有指定 ID 的投資組合
portfolioName string 檢索具有指定名稱的投資組合
portfolioState string 檢索具有指定狀態(tài)的投資組合

請求頭參數(shù):

key value
Authorization access_token
Amazon-Advertising-API-ClientId your client_id
Amazon-Advertising-API-Scope profileId
Content-Type application/json

7.7 代碼實(shí)操

/**
    portfolioIdFilter是portfolioId以逗號拼接到一起的,但是一次最大拼接100個(gè)耗帕。
*/
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("獲取的portfolio = " + s5); 

執(zhí)行結(jié)果就不演示了穆端。

到此sp廣告數(shù)據(jù)已獲取到,處理數(shù)據(jù)保存到文件即可仿便。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末体啰,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子嗽仪,更是在濱河造成了極大的恐慌婉称,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件碾盟,死亡現(xiàn)場離奇詭異逝她,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)窿凤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進(jìn)店門仅偎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人雳殊,你說我怎么就攤上這事橘沥。” “怎么了夯秃?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵座咆,是天一觀的道長。 經(jīng)常有香客問我仓洼,道長箫措,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任衬潦,我火速辦了婚禮斤蔓,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘镀岛。我一直安慰自己弦牡,他們只是感情好友驮,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著驾锰,像睡著了一般卸留。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上椭豫,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天耻瑟,我揣著相機(jī)與錄音,去河邊找鬼赏酥。 笑死喳整,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的裸扶。 我是一名探鬼主播框都,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼呵晨!你這毒婦竟也來了魏保?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤摸屠,失蹤者是張志新(化名)和其女友劉穎谓罗,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體季二,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡妥衣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了戒傻。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,727評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蜂筹,死狀恐怖需纳,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情艺挪,我是刑警寧澤不翩,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站麻裳,受9級特大地震影響口蝠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜津坑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一妙蔗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧疆瑰,春花似錦眉反、人聲如沸昙啄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽梳凛。三九已至,卻和暖如春梳杏,著一層夾襖步出監(jiān)牢的瞬間韧拒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工十性, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留叛溢,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓烁试,卻偏偏與公主長得像雇初,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子减响,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內(nèi)容