一.您还必须在 Visual Studio 中通过 https://visualstudio.microsoft.com/downloads/ 安装以下依赖项:
JsonSubTypes 1.2.0
或者全部Newtonsoft.json 12.0.3
或者全部RestSharp 106.12.0
RateLimiter 2.2.0
或更新版本(这也将安装ComposableAsync.Core
)
二.要完成本教程,您需要满足以下先决条件:
- 处于草稿或已发布状态的混合或 SP-API 应用程序
- 集成开发环境 (IDE) 软件(本演练使用 Windows 操作系统上的 Visual Studio IDE)
三.具体开发步骤:
第 1 步。设置您的工作空间
-
在您的本地驱动器上,为该项目创建一个目录并将其命名
SwaggerToCL
。 -
下载以下工具。
- IDE software (this walkthrough uses Visual Studio IDE on Windows OS)
- GNU Wget
- Java 8 或更高版本
-
运行以下命令下载 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 selling-partner-api-models/models at main · amzn/selling-partner-api-models · GitHub 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.Navigate to the selling-partner-api-models\\models
folder in your local copy of the repository and copy a JSON file from the models subfolders into C:\\SwaggerToCL
. This tutorial uses the Sellers.json file.
7.In GitHub, go to selling-partner-api-models/clients/sellingpartner-api-aa-csharp at main · amzn/selling-partner-api-models · GitHub and download the sellingpartner-api-aa-csharp folder to your local computer.
第 2 步。生成带有 LWA 令牌交换和身份验证的 C# SDK
-
打开 Visual Studio。
-
在
sellingpartner-api-aa-csharp
文件夹,选择SellingPartnerAPIAuthAndAuthCSharp.sln
文件并使用 构建 Visual Studio 中的选项。这会生成Amazon.SellingPartnerAPIAA.dll
在文件夹中组装sellingpartner-api-aa-csharp\\src\\Amazon.SellingPartnerAPIAA\\bin\\Debug\\netstandard2.0
。 -
打开终端并运行以下命令生成 C# 客户端库。生成的客户端库的默认包名称是
Swagger.IO
。这些命令使用各自的 API 名称作为包名称来生成客户端库,而不是Swagger.IO
。- 在
C:\\SwaggerToCL
,创建名为的 JSON 文件csharpConfig.json
,打开编辑器并添加以下代码。对于packageName
,使用您要为其生成客户端库的 API 的相同名称:JSON{"packageName":"SellingPartnerAPI.SellerAPI","targetFramework":"v4.7.2"}
运行此命令生成带有自定义包名称的 C# 代码:
java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\[name of model].json -l csharp -t [path to selling-partner-api-models\clients\sellingpartner-api-aa-csharp folder]\src\Amazon.SellingPartnerAPIAA\resources\swagger-codegen\templates\ -o C:\SwaggerToCL\[name of client library] -c C:\SwaggerToCL\csharpConfig.json
此命令使用
Sellers.json
生成 C# 代码:java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\Sellers.json -l csharp -t C:\SwaggerToCL\sellingpartner-api-aa-csharp\src\Amazon.SellingPartnerAPIAA\resources\swagger-codegen\templates\ -o C:\SwaggerToCL\Sellers_CsharpCL -c C:\SwaggerToCL\csharpConfig.json
SDK 是在中创建的
C:\\SwaggerToCL\\Sellers_CsharpCL
。下一步是编写实际应用程序并使用生成的 C# SDK 连接到 SP-API。 - 在
第 3 步。使用生成的 C# SDK 连接到销售合作伙伴 API
-
In Visual Studio IDE, navigate to your generated library Sellers_CsharpCL folder and open the
.sln
file. The.sln
package file will have the name as provided in Step 2. Generate a C# SDK with LWA token exchange and authentication. In this example, the package file name isSellingPartnerAPI.SellerAPI.sln
. -
右键单击
SellingPartnerAPI.SellerAPI
文件并选择 添加 > 参考文献。 -
在 参考文献 窗口,选择 浏览,导航至
sellingpartner-api-aa csharp/src/Amazon.SellingPartnerAPIAA/bin/Debug/netstandard2.0
然后选择Amazon.SellingPartnerAPIAA.dll
。 -
重复步骤 1-3
SellingPartnerAPI.SellerAPI.Test
文件。 -
导航到生成的
Sellers_CsharpCL
库文件夹,右键单击SellingPartnerAPI.SellerAPI
文件,然后选择选项 > 常规,并将目标框架更改为 .net Framework v4.7 及更高版本。 -
重复第 5 步
SellingPartnerAPI.SellerAPI.Test
文件。 -
右键单击
SellingPartnerAPI.SellerAPI
文件并选择 管理 NuGet 软件包。确保你有以下软件包版本(如果没有,请安装相应的 NuGet 软件包):JsonSubTypes 1.2.0
或者全部Newtonsoft.json 12.0.3
或者全部RestSharp 106.12.0
RateLimiter 2.2.0
或更新版本(这也将安装ComposableAsync.Core
)
-
右键单击
SellingPartnerAPI.SellerAPI.Test
文件并选择 管理 NuGet 软件包。安装 Nunit 2.6.4 包裹。📘
手动安装 NuGet 软件包
在某些情况下(例如,使用 IOS),NuGet 软件包中会显示所需版本,但您可能会遇到错误提示。在这种情况下,必须手动从文件系统中的软件包文件夹添加软件包:
- 在
Sellers_CsharpCL
中,导航到生成的.sln
库文件夹,然后打开 {{2}} 文件。 - 右键单击
SellingPartnerAPI.SellerAPI
文件并选择 添加 > 参考文献。 - 在 参考文献 窗口,选择 浏览,导航至
sellingpartner-api-aa csharp/src/Amazon.SellingPartnerAPIAA/bin/Debug/netstandard2.0
,选择生成的客户端库文件夹(例如,Sellers_CsharpCL
),然后选择 包 然后添加您遇到错误的软件包。
- 在
-
在
SellingPartnerAPI.SellerAPI.Test > Api
文件夹,打开[Modelname]Tests.cs
文件并添加以下代码:using System; using System.IO; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using RestSharp; using SellingPartnerAPI.SellerAPI.Client; using SellingPartnerAPI.SellerAPI.Api; using SellingPartnerAPI.SellerAPI.Model; using Amazon.SellingPartnerAPIAA; namespace SellingPartnerAPI.SellerAPI.Test { class SellersApiTests { public static void Main(string[] args) { try { LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials { ClientId = "amzn1.application-oa2-client.******************", ClientSecret = "***********************************", RefreshToken = "Atzr|***********************************", Endpoint = new Uri("https://api.amazon.com/auth/o2/token") }; SellersApi sellersApi = new SellersApi.Builder() .SetLWAAuthorizationCredentials(lwaAuthorizationCredentials) .Build(); GetMarketplaceParticipationsResponse result = sellersApi.GetMarketplaceParticipations(); Console.WriteLine(result.ToJson()); } catch (LWAException e) { Console.WriteLine("LWA Exception when calling SellersApi#getMarketplaceParticipations"); Console.WriteLine(e.getErrorCode()); Console.WriteLine(e.getErrorMessage()); Console.WriteLine(e.Message); } catch (ApiException e) { Console.WriteLine("Exception when calling SellersApi#getMarketplaceParticipations"); Console.WriteLine(e.Message); } } } }
-
LWAAuthorizationCredentials
实例参数:姓名 描述 必填项 ClientId
Your LWA client identifier. For more information, refer to Viewing your application information and credentials. 有帮助 ClientSecret
Your LWA client secret. For more information, refer to Viewing your application information and credentials. 有帮助 RefreshToken
The LWA refresh token. Get this value when the selling partner authorizes your application. For more information, refer to Authorizing Selling Partner API applications. No. Include RefreshToken
if the operation that you call in the following step requires selling partner authorization. All operations that are not grantless operations require selling partner authorization. If you includeRefreshToken
, do not includeScopes
.Scopes
LWA 授权范围。您可以指定一个或多个
Scopes
值:ScopeNotificationsAPI
. For the Notifications API.ScopeMigrationAPI
. For the Authorization API.
No. Include Scopes
if the operation that you call in the following step is a grantless operation. If you includeScopes
, do not include theRefreshToken
.Endpoint
LWA 身份验证服务器 URI。 有帮助 需要销售合作伙伴授权的操作调用示例:
C#
using Amazon.SellingPartnerAPIAA; LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials { ClientId = "myClientId", ClientSecret = "myClientSecret", RefreshToken = "Aztr|...", Endpoint = new Uri(""https://api.amazon.com/auth/o2/token"") };
无授权操作调用示例:
C#
using Amazon.SellingPartnerAPIAA; LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials { ClientId = "myClientId", ClientSecret = "myClientSecret", Scopes = new List<string>() { ScopeConstants.ScopeNotificationsAPI, ScopeConstants.ScopeMigrationAPI } Endpoint = new Uri(""https://api.amazon.com/auth/o2/token"") };
-
要查看代码的输出,您需要将其转换为控制台应用程序。为此,请右键单击
SellingPartnerAPI.SellerAPI.Test
文件并选择 选项。 -
在 项目选项 窗口,在左侧导航窗格中,展开 构建,然后选择 普通的。在 代码生成 部分,选择 编译目标 然后选择 可通过 GUI 执行。选择 行。
-
生成并运行该项目。