使用生成的 C# SDK 连接到销售伙伴 API, 自动调用 SP-API

一.您还必须在 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 步。设置您的工作空间

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

  2. 下载以下工具。

  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          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

  1. 打开 Visual Studio。

  2. 在 sellingpartner-api-aa-csharp 文件夹,选择 SellingPartnerAPIAuthAndAuthCSharp.sln 文件并使用 构建 Visual Studio 中的选项。这会生成 Amazon.SellingPartnerAPIAA.dll 在文件夹中组装 sellingpartner-api-aa-csharp\\src\\Amazon.SellingPartnerAPIAA\\bin\\Debug\\netstandard2.0

  3. 打开终端并运行以下命令生成 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

  1. 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 is SellingPartnerAPI.SellerAPI.sln.

  2. 右键单击 SellingPartnerAPI.SellerAPI 文件并选择 添加 > 参考文献

  3. 在 参考文献 窗口,选择 浏览,导航至 sellingpartner-api-aa csharp/src/Amazon.SellingPartnerAPIAA/bin/Debug/netstandard2.0 然后选择 Amazon.SellingPartnerAPIAA.dll

  4. 重复步骤 1-3 SellingPartnerAPI.SellerAPI.Test 文件。

  5. 导航到生成的 Sellers_CsharpCL 库文件夹,右键单击 SellingPartnerAPI.SellerAPI 文件,然后选择选项 > 常规,并将目标框架更改为 .net Framework v4.7 及更高版本

  6. 重复第 5 步 SellingPartnerAPI.SellerAPI.Test 文件。

  7. 右键单击 SellingPartnerAPI.SellerAPI 文件并选择 管理 NuGet 软件包。确保你有以下软件包版本(如果没有,请安装相应的 NuGet 软件包):

    • JsonSubTypes 1.2.0 或者全部
    • Newtonsoft.json 12.0.3 或者全部
    • RestSharp 106.12.0
    • RateLimiter 2.2.0 或更新版本(这也将安装 ComposableAsync.Core)
  8. 右键单击 SellingPartnerAPI.SellerAPI.Test 文件并选择 管理 NuGet 软件包。安装 Nunit 2.6.4 包裹。

    📘

    手动安装 NuGet 软件包

    在某些情况下(例如,使用 IOS),NuGet 软件包中会显示所需版本,但您可能会遇到错误提示。在这种情况下,必须手动从文件系统中的软件包文件夹添加软件包:

    1. 在 Sellers_CsharpCL 中,导航到生成的 .sln 库文件夹,然后打开 {{2}} 文件。
    2. 右键单击 SellingPartnerAPI.SellerAPI 文件并选择 添加 > 参考文献
    3. 在 参考文献 窗口,选择 浏览,导航至 sellingpartner-api-aa csharp/src/Amazon.SellingPartnerAPIAA/bin/Debug/netstandard2.0,选择生成的客户端库文件夹(例如, Sellers_CsharpCL),然后选择  然后添加您遇到错误的软件包。
  9. 在 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);
                }
            }
        }
    }

  10. LWAAuthorizationCredentials 实例参数:

    姓名描述必填项
    ClientIdYour LWA client identifier. For more information, refer to Viewing your application information and credentials.有帮助
    ClientSecretYour LWA client secret. For more information, refer to Viewing your application information and credentials.有帮助
    RefreshTokenThe 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 include RefreshToken, do not include Scopes.
    Scopes

    LWA 授权范围。您可以指定一个或多个 Scopes 值:

    No. Include Scopes if the operation that you call in the following step is a grantless operation. If you include Scopes, do not include the RefreshToken.
    EndpointLWA 身份验证服务器 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"") };

  11. 要查看代码的输出,您需要将其转换为控制台应用程序。为此,请右键单击 SellingPartnerAPI.SellerAPI.Test 文件并选择 选项

  12. 在 项目选项 窗口,在左侧导航窗格中,展开 构建,然后选择 普通的。在 代码生成 部分,选择 编译目标 然后选择 可通过 GUI 执行。选择 

  13. 生成并运行该项目。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值