生成されたC# SDKを使用してSelling Partner APIに接続する

生成された C# SDK を使用して SP-API に接続します。

This tutorial describes how to use a generated C# Software Developer Kit (SDK) to make API calls. The SDK exposes classes for configuring your Login with Amazon (LWA) credentials and uses them to generate LWA tokens and sign requests for you. For more information, refer to Generate a C# SDK with LWA token generation and authentication.

Before your application can connect to the Selling Partner API, you must register it and a selling partner must authorize it. Refer to Registering your application and Authorizing Selling Partner API applications.

You must also install the following dependencies via NuGet in Visual Studio:

  • JsonSubTypes 1.2.0またはall
  • Newtonsoft.json 12.0.3またはall
  • RestSharp 106.12.0
  • RateLimiter 2.2.0 またはそれ以降 (これもインストールされます) ComposableAsync.Core)

ステップ3:LWA認証情報を設定する

次のパラメーターを使用して、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

The scope of the LWA authorization grant. Takes the value ScopeNotificationsAPI for the Notifications API.

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。はい

販売パートナーの承認が必要なオペレーションコールの例:

using Amazon.SellingPartnerAPIAA; LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials { ClientId = "myClientId", ClientSecret = "myClientSecret", RefreshToken = "Aztr|...", Endpoint = new Uri(""https://api.amazon.com/auth/o2/token"") };

グラントレスオペレーションの呼び出し例

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"") };

ステップ3:Sellers APIのインスタンスを作成し、オペレーションを呼び出す

After you configure your LWAAuthorizationCredentials, you can create an instance of SellersApi and call an operation.

例:

SellersApi sellersApi = new SellersApi.Builder() .SetLWAAuthorizationCredentials(lwaAuthorizationCredentials) .Build();

C# SDKのトラブルシューティング

IRestRequestエラー:「RestSharp Version=105.0.0.0. Culture=neutral. PublicKevToken=598062e77f915f75は、参照されていないアセンブリに定義されています。アセンブリ{{2}}に参照を追加する必要があります。」

選択すると回答が展開されます。{{0}}パッケージをバージョン106.12.0.0に更新します。参照をチェックして、バージョン106.12が正常に更新されていることを確認してください。引き続き105.0.0.0が表示される場合は、古いバージョンを参照から削除し、生成されたクライアントライブラリでパッケージフォルダーから106.12.0.0パッケージを手動で追加してください。

「型または名前空間の名前{{0}}が見つかりません(使用の指示やアセンブリ参照があることを確認してください)」

回答を表示する場合は、ここを選択してください。

In some scenarios, even though you have the required version in the NuGet packages for Newtonsoft, it requires manual addition from the package folder in the file system. To resolve this, use the following steps:

  1. In Visual Studio, right-click your generated SDK package and choose Add, then References.

  2. On the references window in the .Net Assembly, choose browse.

  3. Navigate to the generated client library folder in local > packages and add the specific package for Newtonsoft.

「メッセージ:フィールドのタイプSellingPartnerAPI.servicesApi.Client.ApiClient:rateLimiterを読み込めませんでした。(3)理由:ファイルまたはアセンブリComposableAsync.Core, Version=1.1.1.0, Culture=neutral, PublicKeyToken=null、またはその依存関係の1つを読み込めませんでした。」

回答を表示する場合は、ここを選択してください。

コードをビルドして実行した後にこのエラーが表示される場合、ComposableAsync.Coredependency packageがダウンロードされましたが、参照として含まれていません。dllを必ず参照として手動で追加してください。

モデルとのマッピングの問題

回答を表示する場合は、ここを選択してください。

The ItemAsin model was generated with the JsonObject type, but the required string ItemAttributes requires the JsonObjectAttribute. You can use this command, which uses the Catalog Items API, to import the required mapping for the models :

`java -jar swagger-codegen-cli.jar generate -i catalogItems_2022-04-01.json -l csharp -t sellingpartner-api-aa-csharp\src\Amazon.SellingPartnerAPIAA\resources\swagger-codegen\templates —import-mappings ItemAttributes=Newtonsoft.Json.JsonObjectAttribute —import-mappings ItemAsin=System.string -o catalogItems2022-04-01_import`

HttpRequestHeaders.ToJson():オーバーライドする適切なメソッドが見つかりませんでした。」

回答を表示する場合は、ここを選択してください。

overrideキーワードを、virtualを使って修正します。メソッドをvirtualとして宣言して、オーバーライドできるようにします。

public virtual string ToJson() { return JsonConvert.SerializeObject(this, Formatting.Indented); }

「現在のコンテキストにBaseValidateという名前は存在しません。」

回答を表示する場合は、ここを選択してください。

クラス内の前述のメソッドのコメントアウトを解除します。

// IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext) // { // foreach (var x in BaseValidate(validationContext)) // yield return x; // yield break; // }

このページは役に立ちましたか?