Amazon使用javaSDK调用SP-API获取卖家分析数据(一)

1,614 阅读3分钟

1.从Amazon下载官方资料

amazon有已经封装好java SDK,可以直接调用API来获取Aor报表。

1.1下载 selling-partner-api-models-main.zip

v2-1737406fe9da751a1f168ba118b1863b_r.jpeg

1.2下载 swagger-codegen-cli-2.4.19.jar

2.解压并获取Java-SDK

2.1解压selling-partner-api-models-main.zip,打开selling-partner-api-models-main/models/sellers-api-model/sellers.json和selling-partner-api-models-main/models/reports-api-model/reports_2020-09-04.json。

2.2将swagger-codegen-cli-2.4.19.jar和sellers.json以及reports_2020-09-04.json放到一个新建的文件夹里面。

2.3使用swagger和xxx.json生成两个Java项目

// [path to selling-partner-api-models\clients\sellingpartner-api-aa-java folder]是指 selling-partner-api-models的下载路径\clients\sellingpartner-api-aa-java
//生成seller项目,这里的seller是一个新建的文件夹
java -jar C:\amazonApi\swagger-codegen-cli.jar generate -i C:\amazonApi\Sellers.json -l java -t [path to selling-partner-api-models\clients\sellingpartner-api-aa-java folder]\resources\swagger-codegen\templates\ -o C:\amazonApi\sellers
//生成repoet项目 ,这里的reports是一个新建的文件夹
java -jar C:\amazonApi\swagger-codegen-cli.jar generate -i C:\amazonApi\reports_2020-09-04.json -l java -t [path to selling-partner-api-models\clients\sellingpartner-api-aa-java folder]\resources\swagger-codegen\templates\ -o C:\amazonApi\reports  

2.4 打开两个项目,打成jar包放到本地maven仓库

给sellers打包过程中缺少SellingPartnerAPIAA依赖

打包之前应该先用idea打开“你下载的selling-partner-api-models-main.zip解压包/clients/sellingpartner-api-aa-java”,然后使用maven将项目install到maven仓库里面 继续回到sellers中尝试进行打包,找不到包com.amazon.SellingPartnerAPIAA,查看依赖发现没有引入,那我们就将起引入即可,然后打包就可以成功了 若给reports打包过程中出现了同样的问题,那么继续引入依赖并打包

3.获取报表下载的Java-SDK

3.1打开selling-partner-api-models-main/clients/sellingpartner-api-documents-helper这个项目,打成jar包放入本地maven仓库

4.新建一个maven项目,引入生成的三个jar包

<dependency>
        <groupId>com.amazon.sellingpartnerapi</groupId>
    <artifactId>sellingpartnerapi-aa-java</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-java-client</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>com.amazon.sellingpartnerapi</groupId>
      <artifactId>sellingpartner-api-documents-helper-java</artifactId>
    <version>1.0.0</version>
</dependency>

5.登陆到report服务器

5.1配置AWS凭证

AWSAuthenticationCredentials awsAuthenticationCredentials = AWSAuthenticationCredentials.builder()
                .accessKeyId("you accessKeyId")
                .secretKey("you secretKey")
                .region("us-east-1")//请求地区
                .build();

5.2 配置AWS凭证提供商

AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider = AWSAuthenticationCredentialsProvider.builder()
                .roleArn("you roleArn")//IAM角色
                .roleSessionName("you roleSessionName")
                .build();

5.3 配置LWA凭证

LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder()
   .clientId("you clientId")
   .clientSecret("you clientSecret")
   .refreshToken("you refreshToken")
   .endpoint("https://api.amazon.com/auth/o2/token")//LWA验证服务器URI
   .build();

5.4 创建reportAPI实例

ReportsApi reportsApi = new ReportsApi.Builder().awsAuthenticationCredentials(awsAuthenticationCredentials)
                .awsAuthenticationCredentialsProvider(awsAuthenticationCredentialsProvider)
                .lwaAuthorizationCredentials(lwaAuthorizationCredentials)
                .endpoint("https://sellingpartnerapi-na.amazon.com")//请求地区
                .build();

6.获取Aor报表

6.1创建报表返回reportId

tip:创建报表比较消耗时间,写测试逻辑的时候建议将创建报表返回的reportId用变量保存起来,然后注释掉这一段(创建报表返回reportId的代码)
CreateReportSpecification body = new CreateReportSpecification();
body.setMarketplaceIds(Arrays.asList("you MarketplaceId","you MarketplaceId"...));            body.setReportType("GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL");
body.setDataStartTime(iso_start);// 示例:iso_start=OffsetDateTime.parse("2021-03-16T00:00:00Z")
body.setDataEndTime(iso_end);
CreateReportResponse report = reportsApi.createReport(body);

如果获取报表出现org.codehaus.stax2.ri.EmptyIterator.getInstance()Ljava/util/Iterator;错误,请添加依赖

<dependency>
    <groupId>org.codehaus.woodstox</groupId>
    <artifactId>stax2-api</artifactId>
    <version>4.1</version>
</dependency>

6.2根据reportId查询创建报表状态并返回reportDocumentId

tip1: 这里获取的状态,最好加一个循环,因为amazon每次响应不一定是正确的
​
tip2: 在循环外定义status的时候,不要定义为空字符串或着null,会出现空指针异常
GetReportResponse apiReport = reportsApi.getReport(reportId);
Report payload = apiReport.getPayload();
String status = payload.getProcessingStatus().getValue();
//如果status为DONE说明创建成功,则会返回reportDocumentId

6.3根据reportDocumentId获取报表下载的url和相关加密参数

 GetReportDocumentResponse reportDocument = reportsApi.getReportDocument(payload.getReportDocumentId());

6.4下载报表

DownloadHelper downloadHelper = new DownloadHelper.Builder().build();
AESCryptoStreamFactory aesCryptoStreamFactory = new AESCryptoStreamFactory.Builder(reportDocument.getPayload().getEncryptionDetails().getKey(), reportDocument.getPayload().getEncryptionDetails().getInitializationVector()).build();
DownloadSpecification downloadSpecification = new DownloadSpecification.Builder(aesCryptoStreamFactory, reportDocument.getPayload().getUrl())                    .withCompressionAlgorithm(CompressionAlgorithm.fromEquivalent(reportDocument.getPayload().getCompressionAlgorithm())).build();
DownloadBundle downloadBundle = downloadHelper.download(downloadSpecification);
BufferedReader reader = downloadBundle.newBufferedReader();
//后续对reader对象进行操作,获取数据生成文件。

PS:大家看了后觉得对自己有帮助可以三连留言,欢迎提出宝贵意见,如想进行技术交流欢迎加入Amazon各类API开发交流群!请添加技术人员微信:x118422邀请进群~