手把手教你从零开始搭建Amazon Advertising-API开发环境(二)之获取SP广告数据

本文主要是介绍手把手教你从零开始搭建Amazon Advertising-API开发环境(二)之获取SP广告数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 获取access_token

官方链接

1.1 请求路径 POST
地区URL
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 请求事例
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 代码实操
 //获取access_token的方法,以NA地区为例。
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);

运行结果如下:

在这里插入图片描述

2. 获取profileId

官方连接

2.1 请求路径 GET
https://advertising-api.amazon.com/v2/profiles
2.2 请求参数
参数名称可能的值(string)
apiProgrambilling, campaign, paymentMethod, store, report, account, posts
accessLeveledit, view
profileTypeFilterseller, vendor, agency
validPaymentMethodFiltertrue, false

请求头:

keyvalue
Content-Typeapplication/json
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id
2.3 代码实操
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);

运行结果如下:

在这里插入图片描述

3. 创建sp_campaign报表

官方链接

⚠️:此次官方文档的Responses有误,大家懂的都懂,已实际的Responses为主。

3.1 请求路径 POST
https://advertising-api.amazon.com/v2/sp/campaigns/report
3.2 请求参数

请求体参数:

keyValue
stateFilterenabled, paused, archived
campaignTypesponsoredProducts
segmentquery, placement
reportDateYYYYMMDD
metrics传入你想获取的值

请求头:

keyvalue
Content-Typeapplication/json
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeprofileId(第二步获取的)
Authorizationaccess_token
3.3 代码实操
/**
*第二步获取的是个List,选择符合条件的进行操作,本次实操选择的是type=seller,profileId=xxxxxxxx,countryCode=CA
*/
String createSpReport = "https://advertising-api.amazon.com/v2/sp/campaigns/report";
//构造请求头
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);
//请求体的参数
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);

运行结果如下:

在这里插入图片描述

4.获取表报下载地址

4.1 请求路径 GET
https://advertising-api.amazon.com/v2/reports/{reportId}
4.2 请求参数

请求头:

keyvalue
Content-Typeapplication/json
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeprofileId
4.3 代码实操
/**当status=SUCCESS的时候说明报表创建好了,这时去获取下载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();

运行结果如下:

在这里插入图片描述

5. 下载报表一

官方链接

官方文档只有在sd广告中才有下载的API

5.1 请求路径 GET

步骤4中获取的downUrl

5.2 请求参数

请求头参数:

keyvalue
Content-Typeapplication/json
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeprofileId
5.3 代码实操
/**
步骤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.下载报表二

6.1 请求路径 GET

步骤5中获取的url

6.2 请求参数

请求头参数:

keyvalue
Accept-Encodinggzip
Acceptapplication/octet-stream
6.3 代码实操
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);

执行结果如下:

在这里插入图片描述

7.根据campaignId获取portfolioId

官方文档

由于步骤六中获取的信息里不包含portfolioId,所以继续获取portfolioId。

7.1 请求路径 GET
https://advertising-api.amazon.com/v2/sp/campaigns
7.2 请求参数

本次请求只传campaignIdFilter参数,

请求头参数:

keyvalue
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeprofileId
Content-Typeapplication/json
7.3 代码实操
/**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);

执行结果就不演示了。

7.4 根据portfolioId去获取portfolio信息

官方文档

7.5 请求路径
https://advertising-api.amazon.com/v2/portfolios
7.6 请求参数
nametype描述
portfolioIdstring检索具有指定 ID 的投资组合
portfolioNamestring检索具有指定名称的投资组合
portfolioStatestring检索具有指定状态的投资组合

请求头参数:

keyvalue
Authorizationaccess_token
Amazon-Advertising-API-ClientIdyour client_id
Amazon-Advertising-API-ScopeprofileId
Content-Typeapplication/json
7.7 代码实操
/**portfolioIdFilter是portfolioId以逗号拼接到一起的,但是一次最大拼接100个。
*/
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); 

执行结果就不演示了。

到此sp广告数据已获取到,处理数据保存到文件即可。

这篇关于手把手教你从零开始搭建Amazon Advertising-API开发环境(二)之获取SP广告数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/310781

相关文章

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils