Mithilfe eines generierten C#-SDK eine Verbindung zur Verkaufspartner-API herstellen
Stellen Sie mithilfe eines generierten C#-SDK eine Verbindung zur SP-API her.
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
oder alleNewtonsoft.json 12.0.3
oder alleRestSharp 106.12.0
RateLimiter 2.2.0
oder neuer (das wird auch installiertComposableAsync.Core
)
Schritt 3. Konfigurieren Sie Ihre LWA-Anmeldeinformationen
Erstellen Sie eine Instanz von LWAAuthorizationCredentials
mit diesen Parametern:
Name | Beschreibung | Erforderlich |
---|---|---|
ClientId | Your LWA client identifier. For more information, refer to Viewing your application information and credentials. | Ja |
ClientSecret | Your LWA client secret. For more information, refer to Viewing your application information and credentials. | Ja |
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 include RefreshToken , do not include Scopes . |
Scopes | The scope of the LWA authorization grant. Takes the value | 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 . |
Endpoint | Die URI des LWA-Authentifizierungsservers. | Ja |
Beispiel für einen Betriebsaufruf, für den eine Genehmigung durch den Vertriebspartner erforderlich ist:
using Amazon.SellingPartnerAPIAA;
LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials
{
ClientId = "myClientId",
ClientSecret = "myClientSecret",
RefreshToken = "Aztr|...",
Endpoint = new Uri(""https://api.amazon.com/auth/o2/token"")
};
Beispiel für einen berechtigungslosen Vorgangsaufruf
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"")
};
Schritt 3. Erstellen Sie eine Instanz der Verkäufer-API und rufen Sie einen Vorgang auf
After you configure your LWAAuthorizationCredentials
, you can create an instance of SellersApi
and call an operation.
Beispiel:
SellersApi sellersApi = new SellersApi.Builder()
.SetLWAAuthorizationCredentials(lwaAuthorizationCredentials)
.Build();
Problembehandlung bei einem C#-SDK
IRestRequest
Fehler:“RestSharp Version=105.0.0.0. Culture=neutral. PublicKevToken=598062e77f915f75
ist in einer Assembly definiert, auf die nicht verwiesen wird. Sie müssen einen Verweis auf die Assembly hinzufügen {{2}}“
Auswählen, um die Antwort zu erweitern.
Aktualisieren Sie das Paket {{0}} auf Version 106.12.0.0. Überprüfen Sie die Referenz, um sicherzustellen, dass die Version 106.12 erfolgreich aktualisiert wurde. Wenn weiterhin 105.0.0.0 angezeigt wird, entfernen Sie die alte Version aus der Referenz und fügen Sie das Paket 106.12.0.0 manuell aus dem Ordner Pakete mit der generierten Clientbibliothek hinzu."Der Typ- oder Namespace-Name {{0}} konnte nicht gefunden werden (using-Direktive oder Assembler-Referenz nicht vorhanden?)"
Wählen Sie aus, um die Antwort zu erweitern.
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:
-
In Visual Studio, right-click your generated SDK package and choose
Add
, thenReferences
. -
On the references window in the .Net Assembly, choose
browse
. -
Navigate to the generated client library folder in local > packages and add the specific package for Newtonsoft.
"Meldung: Feldtyp SellingPartnerAPI.servicesApi.Client.ApiClient:rateLimiter
(3) konnte nicht geladen werden, aufgrund von: Konnte Datei oder Assembly ComposableAsync.Core, Version=1.1.1.0, Culture=neutral, PublicKeyToken=null
oder eine ihrer Abhängigkeiten nicht laden."
Wählen Sie aus, um die Antwort zu erweitern.
Wenn Sie diesen Fehler erhalten, nachdem Sie Ihren Code erstellt und ausgeführt haben, wurde das ComposableAsync.Core
-Abhängigkeitspaket zwar heruntergeladen, aber nicht als Referenz hinzugefügt. Stellen Sie sicher, dass Sie die dll manuell als Referenz hinzufügen.
Probleme bei der Zuordnung von Modellen
Wählen Sie aus, um die Antwort zu erweitern.
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()
: Es wurde keine geeignete Methode zum Überschreiben gefunden."
Wählen Sie aus, um die Antwort zu erweitern.
Ändern Sie das Schlüsselwort override
mit virtual
. Deklarieren Sie Methoden als virtual
, damit sie überschrieben werden können.
public virtual string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
"Der Name BaseValidate
existiert im aktuellen Kontext nicht"
Wählen Sie aus, um die Antwort zu erweitern.
Heben Sie die Kommentierung der genannten Methoden in der Klasse auf:
// IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
// {
// foreach (var x in BaseValidate(validationContext))
// yield return x;
// yield break;
// }
Updated 23 days ago