主页文档代码示例API 参考公告模型发行说明常见问题GitHub视频
开发者中心API 状态支持
文档
开发者中心API 状态支持

教程:使用 Java SDK 自动调用 SP-API

借助“使用亚马逊账户登录” (LWA) 令牌交换和身份验证,自动执行 SP-API 调用。

本教程介绍如何使用 “用亚马逊账户登录” (LWA) 令牌交换和身份验证生成 Java SDK,以无缝构建应用程序。您将学习构建 Java SDK 所需的先决条件,并使用卖家销售合作伙伴 API 和 Swagger 代码生成器查看示例。

首先,看看授权过程是如何运作的。

教程

以下教程将帮助您设置自己的 Java 开发工具包来自动执行 SP-API 调用。

先决条件

要完成本教程,您需要满足以下前提条件:

  • 处于草稿或发布状态的混合或 SP-API 应用
  • 集成开发环境 (IDE) 软件(本演练使用 Windows 操作系统上的 Eclipse IDE)

Before your application can connect to the Selling Partner API, you must register it, and it must be authorized by a selling partner. If you do not have a hybrid or SP-API app, follow the steps to register as a developer, register your application, and Authorizing Selling Partner API applications. Then, return to this tutorial.

接下来,为教程设置工作区。

第 1 步:设置工作区

首先,你需要下载所需的软件包,下载 Swagger 代码生成器,并克隆 SP-API 模型存储库。

  1. 在您的本地驱动器上,为该项目创建一个目录并将其命名 SwaggerToCL

  2. 下载以下工具并将其发布到您的 $PATH

  3. 运行以下命令下载 Swagger 代码生成器:

    wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.13/swagger-codegen-cli-2.4.13.jar -O swagger-codegen-cli.jar
  4. swagger-codegen-cli.jar 复制到本地目录 C:\\SwaggerToCL 中。

  5. In GitHub, go to https://github.com/amzn/selling-partner-api-models/tree/main/models and use the following command to clone the selling-partner-api-models repository to your local directory C:\\SwaggerToCL.

    git clone https://github.com/amzn/selling-partner-api-models
  6. 导航到 selling-partner-api-models\models 在存储库的本地副本中添加文件夹,并将模型子文件夹中的所有 JSON 文件复制到 C:\\SwaggerToCL

    The selling-partner-api-models repository has two directories:

    • models directory - contains all the currently available Selling Partner API models.

    📘

    注意

    Developers use Swagger Codegen to generate client libraries from these models. Swagger CodeGen is an open source project which enables automatic generation of API client libraries (SDK generation), server stubs, and documentation from an OpenAPI specification. For more information, refer to Swagger Codegen.

Now that you have completed the required setup, the next step is to generate the Java SDK with the authentication and authorization classes provided in the sellingpartner-api-aa-java folder.

第 2 步。为 SP-API 生成 Java SDK

在此步骤中,您将根据文件夹中的模板生成 SDK selling-partner-api-models\clients\sellingpartner-api-aa-java 在存储库的本地副本中。此文件夹包含授权和身份验证库、Swagger 代码生成器的自定义模板以及用于生成正确版本的 SDK 的配置文件。

要生成 Java SDK,您需要为本地目录中的每个 JSON 文件运行以下命令:

java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate \ -i C:\SwaggerToCL\[name of model].json \ -l java -t [path to selling-partner-api-models\clients\sellingpartner-api-aa-java folder]\resources\swagger-codegen\templates\ \ -o C:\SwaggerToCL\[name of client library] \ -c [path to selling-partner-api-models\clients\sellingpartner-api-aa-java folder]\resources\java\config.json

例如,运行以下命令为生成 Java 代码 sellers.json

java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate \ -i C:\SwaggerToCL\Sellers.json \ -l java -t [path to selling-partner-api-models\clients\sellingpartner-api-aa-java folder]\resources\swagger-codegen\templates\ \ -o C:\SwaggerToCL\Sellers_JavaCL \ -c [path to selling-partner-api-models\clients\sellingpartner-api-aa-java folder]\resources\java\config.json

Repeat this step to generate SDKs for each of the SP-APIs available in the models directory.

生成的 SDK 包含用于配置 LWA 凭证的类,并使用它们来交换 LWA 令牌。

In the following steps, x.x refers to the latest version of the AA library. Refer to the readme to get the latest version.

第 3 步。构建身份验证/授权库 (AA) 库

生成 SDK 后,构建 AA 库。

  1. 导航到 selling-partner-api-models\clients\sellingpartner-api-aa-java 存储库的本地副本的文件夹,然后运行 mvn package。这将生成一个名为的文件夹 target。这个文件夹里有一个 JAR 文件名为 sellingpartnerapi-aa-java-x.x-jar-with-dependencies.jar (或类似名称)和所有必需的依赖关系。
  2. 运行以下命令,将 JAR 文件安装到本地 {{1}} 存储库中。
mvn install:install-file -Dfile=[path to JAR file in "target" folder] -DgroupId=com.amazon.sellingpartnerapi -DartifactId=sellingpartnerapi-aa-java -Dversion=x.x -Dpackaging=jar

你可以找到真正的 groupIdartifactId,以及 version 顶部附近的值 pom.xml 文件在 selling-partner-api-models\clients\sellingpartner-api-aa-java 文件夹。

示例:

mvn install:install-file -Dfile=C:\SwaggerToCL\sellingpartner-api-aa-java\target\sellingpartnerapi-aa-java-x.x.jar -DgroupId=com.amazon.sellingpartnerapi -DartifactId=sellingpartnerapi-aa-java -Dversion=x.x -Dpackaging=jar
mvn install:install-file -Dfile=C:\SwaggerToCL\sellingpartner-api-aa-java\target\sellingpartnerapi-aa-java-x.x-jar-with-dependencies.jar -DgroupId=com.amazon.sellingpartnerapi -DartifactId=sellingpartnerapi-aa-java -Dversion=x.x -Dpackaging=jar

第 4 步。将库添加为 SDK 的依赖项

要将该库添加为依赖项,您需要编辑 pom.xml 文件。

  1. 在您的本地目录中,导航到 selling-partner-api-models/clients/sellingpartner-api-aa-java 然后打开 pom.xml 文件。

  2. 将以下代码作为依赖项添加到文件中。

    <dependency> <groupId>com.amazon.sellingpartnerapi</groupId> <artifactId>sellingpartnerapi-aa-java</artifactId> <version>x.x</version> </dependency>
  3. 保存并关闭该文件。

下一步是编写实际的应用程序,并使用生成的 Java SDK 连接到 SP-API。

第 5 步。使用生成的 Java SDK 连接到销售合作伙伴 API

In this step, you connect to the Selling Partner API using your generated Java SDK. These steps use Eclipse IDE to import the generated SDK and connect to the Selling Partner API, but you can use your preferred IDE.

  1. 在 {{0}} 中的文件菜单上,选择从文件系统打开项目,然后导入生成的 Java SDK。

  2. src/main/java 添加一个 class 来写你的代码。

  3. 在添加的代码中输入代码 class 配置以下实例:

    📘

    注意

    注意:如果类路径无法识别 JAR 中的类,则需要添加在 {{2}} 文件夹(位于生成的 SDK 文件夹中)中创建的 target 文件。

  4. 创建生成的 SDK API 的实例并调用操作。有关示例,请参阅以下用于连接卖家销售合作伙伴 API 的示例代码(前提是您为此 API 生成了 SDK)。

    import io.swagger.client.*; import io.swagger.client.auth.*; import io.swagger.client.model.*; import io.swagger.client.api.SellersApi; import java.io.File; import java.util.*; import com.amazon.SellingPartnerAPIAA.LWAAuthorizationCredentials; import com.amazon.SellingPartnerAPIAA.LWAException; import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_NOTIFICATIONS_API; // for grantless operations (Notifications API) import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_MIGRATION_API; // for grantless operations (Authorization API) public class SellersApiDemo { public static void main(String[] args) { //Configure your LWA credentials : LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder() .clientId("amzn1.application-*********************") .clientSecret("***********************************") .refreshToken("Atzr|******************************") .endpoint("https://api.amazon.com/auth/o2/token") .build(); //For Grantless operations (Authorization API and Notifications API), use following code : /* LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder() .clientId("amzn1.application-*********************") .clientSecret("***********************************") .withScopes(SCOPE_NOTIFICATIONS_API, SCOPE_MIGRATION_API) .endpoint("https://api.amazon.com/auth/o2/token") .build(); */ //Create an instance of the Sellers API and call an operation : SellersApi sellersApi = new SellersApi.Builder() .lwaAuthorizationCredentials(lwaAuthorizationCredentials) .endpoint("https://sellingpartnerapi-na.amazon.com") // use Sandbox URL here if you would like to test your applications without affecting production data. .build(); try { GetMarketplaceParticipationsResponse result = sellersApi.getMarketplaceParticipations(); System.out.println(result); } catch (ApiException e) { System.err.println("Exception when calling SellersApi#getMarketplaceParticipations"); e.printStackTrace(); } catch (LWAException e) { System.err.println("LWA Exception when calling SellersApi#getMarketplaceParticipations"); System.err.println(e.getErrorCode()); System.err.println(e.getErrorMessage()); e.printStackTrace(); } } }

运行代码,然后运行 main 类,您应该会看到授权卖家可以在其中开展销售的商城列表。

对于其他 SP-API,您可以遵循类似的步骤。


此页面对您有帮助吗?