Connessione all'API per i partner di vendita utilizzando un SDK C# generato
Connettiti all'API SP utilizzando un SDK C# generato.
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
o tuttiNewtonsoft.json 12.0.3
o tuttiRestSharp 106.12.0
RateLimiter 2.2.0
o più recente (verrà installato anche questoComposableAsync.Core
)
Passaggio 3. Configura le tue credenziali LWA
Crea un'istanza di LWAAuthorizationCredentials
utilizzando questi parametri:
Nome | Descrizione | Obbligatorio |
---|---|---|
ClientId | Your LWA client identifier. For more information, refer to Viewing your application information and credentials. | Sì |
ClientSecret | Your LWA client secret. For more information, refer to Viewing your application information and credentials. | Sì |
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 | L'URI del server di autenticazione LWA. | Sì |
Esempio di chiamata operativa che richiede l'autorizzazione del partner di vendita:
using Amazon.SellingPartnerAPIAA;
LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials
{
ClientId = "myClientId",
ClientSecret = "myClientSecret",
RefreshToken = "Aztr|...",
Endpoint = new Uri(""https://api.amazon.com/auth/o2/token"")
};
Esempio di chiamata operativa senza concessione
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"")
};
Passaggio 3. Crea un'istanza dell'API Sellers e chiama un'operazione
After you configure your LWAAuthorizationCredentials
, you can create an instance of SellersApi
and call an operation.
Esempio:
SellersApi sellersApi = new SellersApi.Builder()
.SetLWAAuthorizationCredentials(lwaAuthorizationCredentials)
.Build();
Risolvere i problemi relativi a un SDK C#
IRestRequest
errori: "RestSharp Version=105.0.0.0. Culture=neutral. PublicKevToken=598062e77f915f75
è definito in un insieme a cui non si fa riferimento. È necessario aggiungere un riferimento all'insieme {{2}}"
Seleziona per espandere la risposta.
Aggiorna il pacchetto {{0}} alla versione 106.12.0.0. Controlla il riferimento per assicurarti che la versione 106.12 sia stata aggiornata correttamente. Se continua a mostrare 105.0.0.0, rimuovi la vecchia versione dal riferimento e aggiungi il pacchetto 106.12.0.0 manualmente dalla cartella dei pacchetti con la libreria del cliente generata."Il nome del tipo o dello spazio dei nomi {{0}} non è stato trovato (ti manca una direttiva di utilizzo o un riferimento all'assembly?)"
Seleziona per espandere la risposta.
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.
"Messaggio: impossibile caricare il tipo di campo SellingPartnerAPI.servicesApi.Client.ApiClient:rateLimiter
(3) a causa di: Impossibile caricare il file o l'assembly ComposableAsync.Core, Version=1.1.1.0, Culture=neutral, PublicKeyToken=null
o una delle sue dipendenze".
Seleziona per espandere la risposta.
Se ricevi questo errore dopo aver creato ed eseguito il codice, allora il pacchetto di dipendenze ComposableAsync.Core
è stato scaricato ma non incluso come riferimento. Assicurati di aggiungere la dll come riferimento manualmente.
Problemi di mappatura con i modelli
Seleziona per espandere la risposta.
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()
: non è stato trovato alcun metodo adatto per eseguire l'override."
Seleziona per espandere la risposta.
Modifica la parola chiave di override
con virtual
. Dichiara i metodi come virtual
in modo che possano essere sovrascritti.
public virtual string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
"Il nome BaseValidate
non esiste nel contesto attuale"
Seleziona per espandere la risposta.
Rimuovi il commento ai metodi menzionati nella classe:
// IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
// {
// foreach (var x in BaseValidate(validationContext))
// yield return x;
// yield break;
// }
Updated 23 days ago