对接Amazon平台API

官网文档Selling Partner API (amazon.com)

创建账号,获取accessKeyId、secretKey、Arn

获取clientId、clientSecret、refreshToken

手把手教你从零开始搭建Amazon SP-API开发环境(原MWS API) - 简书 (jianshu.com)

获取到sdk,并且通过以上的信息,进行测试调用

Amazon平台SP-API的SDK包生成和使用详解-CSDN博客

[亚马逊官方api(SellingPartner - API)帮助文档_亚马逊接口文档-CSDN博客](https://blog.csdn.net/mollen/article/details/132697933#:~:text=亚马逊官方api(SellingPartner - API)帮助文档 1 1、什么是销售伙伴 API? 销售合作伙伴 API,5 5、查看您的开发者信息 … 6 6、授权销售伙伴 API 应用程序 )

Amazon使用javaSDK调用SP-API获取卖家分析数据(二)ordersV0Api - 简书 (jianshu.com)

参考

对接amazon sp-api-开发者权限配置(一) - 掘金 (juejin.cn)

对接amazon sp-spi-接口对接(二) - 掘金 (juejin.cn)

AWS 之 IAM - 掘金 (juejin.cn)

中文解释 英文名称 英文说明 来源
AWS访问密钥编码 accessKeyId AWS access key Id 创建新的IAM用户之后
AWS访问密钥 secretKey AWS secret access key 创建新的IAM用户之后
IAM职权ARN roleArn ARN of the IAM role 创建IAM role的时候生成
LWA客户端编码 clientId LWA client identifier 在注册应用程序时生成
LWA客户端秘钥 clientSecret LWA client secret 在注册应用程序时生成
LWA客户端令牌 refreshToken LWA refresh token 在给应用程序授权时生成

实战代码:

package com.xinghuo.service.api.amazon;


import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentials;
import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentialsProvider;
import com.amazon.SellingPartnerAPIAA.LWAAuthorizationCredentials;
import com.amazon.SellingPartnerAPIAA.ScopeConstants;
import com.xinghuo.framework.core.util.ResultData;
import io.swagger.client.sellerorders.ApiException;
import io.swagger.client.sellerorders.api.OrdersV0Api;
import io.swagger.client.sellerorders.model.GetOrdersResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_MIGRATION_API;
import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_NOTIFICATIONS_API;

@RestController
@RequestMapping("/amazon")
public class AmazonApi {
@Value("${amazon.clientId}")
private String clientId;
@Value("${amazon.clientSecret}")
private String clientSecret;
@Value("${amazon.refreshToken}")
private String refreshToken;
@Value("${amazon.accessKeyId}")
private String accessKeyId;
@Value("${amazon.secretKey}")
private String secretKey;
@Value("${amazon.roleArn}")
private String roleArn;
@Value("${amazon.sandBoxEndpoint}")
private String sandBoxEndpoint;
@Value("${amazon.endpoint}")
private String endpoint;
@Value("${amazon.tokenUrl}")
private String tokenUrl;
@Value("${amazon.region}")
private String region;

/**
* 1连接到spApi
* 配置自己的AWS凭证
*/
//注意这个地方的region分北美,欧洲,远东三个AWS区域
public static AWSAuthenticationCredentials getAWSAuthenticationCredentials(String accessKeyId, String secretKey, String region) {
AWSAuthenticationCredentials awsAuthenticationCredentials = AWSAuthenticationCredentials.builder()
//注册成为开发者时生成的AWS访问密钥ID
.accessKeyId(accessKeyId)
//注册成为开发者时生成的AWS访问密钥
.secretKey(secretKey)
//注意,这里的region分北美(us-east-1),欧洲(eu-west-1),远东(us-west-2)
.region(region)
.build();
return awsAuthenticationCredentials;
}

/**
* 2配置您的AWS凭证提供商
*/
public static AWSAuthenticationCredentialsProvider getAWSAuthenticationCredentialsProvider(String roleArn) {
AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider = AWSAuthenticationCredentialsProvider.builder()
//创建IAM职权的时候会生成这个ARN
.roleArn(roleArn)
//唯一值,可以使用UUID
.roleSessionName(UUID.randomUUID().toString())
.build();
return awsAuthenticationCredentialsProvider;
}

/**
* 3配置LWA凭证
*/
public static LWAAuthorizationCredentials getLWAAuthorizationCredentials(String clientId, String clientSecret, String refreshToken, String tokenUrl) {
LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder()
//查看开发者信息的时候可看到LWA的客户端编码
.clientId(clientId)
//查看开发者信息的时候可看到LWA的客户端秘钥
.clientSecret(clientSecret)
//根据上面的客户端编码和客户端秘钥请求客户端令牌
// .withScopes(ScopeConstants.SCOPE_NOTIFICATIONS_API, ScopeConstants.SCOPE_MIGRATION_API)
.refreshToken(refreshToken)
//"https://api.amazon.com/auth/o2/token"
.endpoint(tokenUrl)
.build();
return lwaAuthorizationCredentials;
}

/**
* 获取新增的订单
*/
@RequestMapping("/getOrders")
@Transactional
public ResultData getOrders() {
OrdersV0Api ordersV0Api = new OrdersV0Api.Builder().awsAuthenticationCredentials(getAWSAuthenticationCredentials(accessKeyId, secretKey, region))
.awsAuthenticationCredentialsProvider(getAWSAuthenticationCredentialsProvider(roleArn))
.lwaAuthorizationCredentials(getLWAAuthorizationCredentials(clientId, clientSecret, refreshToken, tokenUrl))
//本次试验为测试环境
//注意,这里的endpoint分北美,欧洲,远东三个地域,每个区域的链接是不一样的
//北美,https://sellingpartnerapi-na.amazon.com
//欧洲,https://sellingpartnerapi-eu.amazon.com
//远东,https://sellingpartnerapi-fe.amazon.com
.endpoint(endpoint)
.build();

List<String> marketplaceIds = Arrays.asList("ATVPDKIKX0DER");
GetOrdersResponse orders = null;
try {
orders = ordersV0Api.getOrders(marketplaceIds, "TEST_CASE_200", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
} catch (ApiException e) {
e.printStackTrace();
return ResultData.fail(e.getMessage());
// System.out.println("orders.getErrors().toString() = " + orders.getErrors().toString());
}
return ResultData.succeed("orders.getPayload().getOrders() = " + orders.getPayload());
}

public static void main(String[] args) throws ApiException {
}
}