对接亚马逊 SP-API(Amazon Selling Partner API) 第三章:对接 SDK
1.Generating a Java SDK with LWA token exchange and authentication2.Connecting to the Selling Parter API using a generated Java SDK3.Generating a Java client library4.Connecting to the Selling Partner
目录
1. Generating a Java SDK with LWA token exchange and authentication
1.10.1 导航到 selling-partner-api-models\clients\sellingpartner-api-aa-java 文件夹并运行 mvn 软件包。
1.10.2 将 sellingpartnerapi-aa-java-1.0-jar-with-dependencies.jar 安装到本地 Maven 仓库。
2. Connecting to the Selling Partner API using a generated Java SDK
2.1. 在【Sellers_JavaCL】项目 pom.xml 中添加了 maven 依赖后,测试。
3. Generating a Java client library
4. Connecting to the Selling Partner API
Step 1. Request a Login with Amazon access token
Step 2. Construct a Selling Partner API URI
Step 3. Add headers to the URI
Step 4. Create and sign your request
对接亚马逊 SP-API(Selling Partner API) 第四章:签名
必要条件
Java 8 或更新版本 和 Apache Maven 3.6 或更高版本
1. Generating a Java SDK with LWA token exchange and authentication
该 SDK 提供了用于配置 LWA 和 AWS 凭证的类。
下列步骤与文档步骤相对应。
1.8. SwaggerToCL 结构.
以后需要用到哪个模块的 SDK,将 json 文件替换了就可以了。
1.9. 根据模板生成 SDK
java -jar E:\workspace\github\sp-api\SwaggerToCL\swagger-codegen-cli.jar generate -i E:\workspace\github\sp-api\Sellers.json -l java -t E:\workspace\github\sp-api\selling-partner-api-models\clients\sellingpartner-api-aa-java\resources\swagger-codegen\templates\ -o E:\SwaggerToCL\Sellers_Java
参数说明:
-t | selling-partner-api-models\clients\sellingpartner-api-aa-java 路径 |
-o | 定义了文件的生成位置 |
-i | Sellers.json 文件忽略大小写 |
BUG 集合
1. 因为我改过目录,所以找不到 swagger-codegen-cli.jar
2. 需要将 jar 包名字改成 swagger-codegen-cli.jar。因为下载的是
1.9.1. Sellers_JavaCL 效果图
1.10.1 导航到 selling-partner-api-models\clients\sellingpartner-api-aa-java 文件夹并运行 mvn 软件包。
因为我是用 IDEA 的,所以直接打开 【sellingpartner-api-aa-java】项目,在 maven 导航栏中执行 package(红色框)。然后找到 sellingpartnerapi-aa-java-1.0-jar-with-dependencies.jar (绿色框)
1.10.2 将 sellingpartnerapi-aa-java-1.0-jar-with-dependencies.jar 安装到本地 Maven 仓库。
mvn install:install-file -Dfile=E:\workspace\github\sp-api\selling-partner-api-models\clients\sellingpartner-api-aa-java\target\sellingpartnerapi-aa-java-1.0-jar-with-dependencies.jar -DgroupId=com.amazon.sellingpartnerapi -DartifactId=sellingpartnerapi-aa-java -Dversion=1.0 -Dpackaging=jar
-Dfile | sellingpartnerapi-aa-java-1.0-jar-with-dependencies.jar 完整路径 |
-DgroupId | groupId |
-DartifactId | artifactId |
-Dversion | version |
2. Connecting to the Selling Partner API using a generated Java SDK
这一部分的中英文档似乎有些不一样。本文测试案例使用的是英文文档的教程。
2.1. 在【Sellers_JavaCL】项目 pom.xml 中添加了 maven 依赖后,测试。
2.2. 测试文件(不同区域记得更换 region)
/*
* Selling Partner API for Sellers
* The Selling Partner API for Sellers lets you retrieve information on behalf of sellers about their seller account, such as the marketplaces they participate in. Along with listing the marketplaces that a seller can sell in, the API also provides additional information about the marketplace such as the default language and the default currency. The API also provides seller-specific information such as whether the seller has suspended listings in that marketplace.
*
* OpenAPI spec version: v1
*
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
package io.swagger.client.api;
import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentials;
import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentialsProvider;
import com.amazon.SellingPartnerAPIAA.LWAAuthorizationCredentials;
import io.swagger.client.ApiException;
import io.swagger.client.model.GetMarketplaceParticipationsResponse;
import org.junit.Test;
import org.junit.Ignore;
import java.util.*;
/**
* API tests for SellersApi
*/
@Ignore
public class SellersApiTest {
// private final SellersApi api = new SellersApi();
private static final String USER_ACCESS_KEY_ID = "A4K";
private static final String USER_SECRET_ACCESS_KEY = "kzqHHaXKiYKAta";
private static final String ROLE_ARN = "arn:aws:iam::5253:role/SP-API";
private static final String APP_CLIENT_ID = "amzn1.application-oa2-cecf5";
private static final String APP_CLIENT_SECRET = "2ccbd3f7538106e0ec";
private static final String REFRESH_TOKEN = "Atzr|IwEBIPHGzYdNQda8Iqrj7gMWKFhQygchk";
/**
* Returns a list of marketplaces that the seller submitting the request can sell in and information about the seller's participation in those marketplaces. **Usage Plan:** | Rate (requests per second) | Burst | | ---- | ---- | | .016 | 15 | For more information, see \"Usage Plans and Rate Limits\" in the Selling Partner API documentation.
*
* @throws ApiException if the Api call fails
*/
@Test
public void getMarketplaceParticipationsTest() throws ApiException {
// GetMarketplaceParticipationsResponse response = api.getMarketplaceParticipations();
// TODO: test validations
/*
* user credentials
* */
AWSAuthenticationCredentials awsAuthenticationCredentials = AWSAuthenticationCredentials.builder()
.accessKeyId(USER_ACCESS_KEY_ID)
.secretKey(USER_SECRET_ACCESS_KEY)
.region("us-east-1")
.build();
/*
* user IAM ARN
* */
AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider = AWSAuthenticationCredentialsProvider.builder()
.roleArn(ROLE_ARN)
.roleSessionName(getRandomNonce())
.build();
/*
* application
* */
LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder()
.clientId(APP_CLIENT_ID)
.clientSecret(APP_CLIENT_SECRET)
.refreshToken(REFRESH_TOKEN)
.endpoint("https://api.amazon.com/auth/o2/token")
.build();
SellersApi sellersApi = new SellersApi.Builder()
.awsAuthenticationCredentials(awsAuthenticationCredentials)
.lwaAuthorizationCredentials(lwaAuthorizationCredentials)
.awsAuthenticationCredentialsProvider(awsAuthenticationCredentialsProvider)
.endpoint("https://sellingpartnerapi-na.amazon.com")
.build();
System.out.println(sellersApi.getMarketplaceParticipations());
}
public String getRandomNonce() {
return UUID.randomUUID().toString().replace("-", "");
}
}
输出结果如下
3. Generating a Java client library
恕我眼拙,除了没有添加到 Maven 仓库,和第一节的内容好像是一样的?我直接跳过了。
4. Connecting to the Selling Partner API
这一部分是展示了如何拼接一个完整的 http 请求。可先跳过,我打算下两章【签名】【Reports 模块】,结合起来根据实际请求做 Demo。
Step 1. Request a Login with Amazon access token
Step 2. Construct a Selling Partner API URI
Step 3. Add headers to the URI
Step 4. Create and sign your request
签名如何构建。Java 可以参考 AWS4Signer.java。其他语言在 AWS GitHub repository 找对应的类。
总结
这一章介绍如何生成各个模块的SDK
对接亚马逊 SP-API(Amazon Selling Partner API) 第四章:签名
更多推荐
所有评论(0)