对接亚马逊 SP-API(Amazon Selling Partner API) 第三章:对接 SDK

必要条件

 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

 参数说明:

-tselling-partner-api-models\clients\sellingpartner-api-aa-java 路径
-o定义了文件的生成位置
-iSellers.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

-Dfilesellingpartnerapi-aa-java-1.0-jar-with-dependencies.jar 完整路径
-DgroupIdgroupId
-DartifactIdartifactId
-Dversionversion

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

官方文档:Signing AWS API requests

签名如何构建。Java 可以参考 AWS4Signer.java。其他语言在 AWS GitHub repository 找对应的类。

总结

这一章介绍如何生成各个模块的SDK

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
com.jd.open.api:open-api-sdk:2.0是京东开放平台的Java开发工具包,用于开发者与京东开放平台进行对接和交互的SDK。 京东开放平台是京东商城提供给商家和开发者的一套开放平台服务,包括商品查询、订单管理、用户授权、营销推广等功能。开发者通过使用open-api-sdk可以方便地使用京东开放平台的各种接口,节省开发时间和精力。 open-api-sdk的版本号为2.0,表示这已经是该工具包的第二个大版本,在之前版本的基础上进行了更新和改进。新版本的sdk通常包含更多功能、修复了之前版本中的bug,并提供更好的兼容性和稳定性。 使用com.jd.open.api:open-api-sdk:2.0可以通过调用相应的接口实现与京东开放平台的连接和数据交互。例如,开发者可以使用该工具包中提供的接口发送商品查询请求,获取商品的详细信息;也可以使用接口进行订单管理,包括订单创建、取消、查询等操作。 此外,open-api-sdk还提供了其他一些功能,如用户授权、优惠券领取与使用、营销推广等。开发者可以根据自己的需求选择使用相应的接口,与京东开放平台进行集成开发。 总之,com.jd.open.api:open-api-sdk:2.0是京东开放平台提供给开发者的Java开发工具包,可以方便地与京东开放平台进行对接和交互,实现商品查询、订单管理、用户授权等功能。开发者可以根据具体需求使用该工具包中提供的接口,进行开发和集成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值