Dart
Dart
Amazon SP-API Sellers Get Marketplace Participations
See more Amazon SP-API Examples
Demonstrates Amazon SP-API Sellers API -- get marketplace participations.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final authAws = CkAuthAws();
authAws.accessKey = 'AWS_ACCESS_KEY';
authAws.secretKey = 'AWS_SECRET_KEY';
authAws.serviceName = 'execute-api';
// Use the region that is correct for your needs.
authAws.region = 'eu-west-1';
final rest = CkRest();
final bTls = true;
final port = 443;
final bAutoReconnect = true;
// Make sure to use the correct domain.
// In this example, we are using "sandbox.sellingpartnerapi-eu.amazon.com"
try {
rest.connect('sandbox.sellingpartnerapi-eu.amazon.com', port, bTls, bAutoReconnect);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
rest.setAuthAws(authAws);
// Load the previously obtained LWA access token.
// See Fetch SP-API LWA Access Token
final jsonToken = CkJsonObject();
try {
jsonToken.loadFile('qa_data/tokens/sp_api_lwa_token.json');
} on ChilkatException {
print('Failed to load LWA access token.');
return;
}
// Add the x-amz-access-token request header.
final lwaToken = jsonToken.stringOf('access_token');
rest.clearAllHeaders();
rest.addHeader('x-amz-access-token', lwaToken);
final sbResponse = CkStringBuilder();
final uri = '/sellers/v1/marketplaceParticipations';
try {
rest.fullRequestNoBodySb('GET', uri, sbResponse);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Examine the response status.
final statusCode = rest.responseStatusCode;
if (statusCode != 200) {
print('Response status text: ${rest.responseStatusText}');
print('Response body: ');
print(sbResponse.getAsString());
print('Failed.');
return;
}
print(sbResponse.getAsString());
// If successful, gets a JSON response such as the following:
// {
// "payload": [
// {
// "marketplace": {
// "id": "ATVPDKIKX0DER",
// "countryCode": "US",
// "name": "Amazon.com",
// "defaultCurrencyCode": "USD",
// "defaultLanguageCode": "en_US",
// "domainName": "www.amazon.com"
// },
// "participation": {
// "isParticipating": true,
// "hasSuspendedListings": false
// }
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
final json = CkJsonObject();
json.loadSb(sbResponse);
var id = '';
var countryCode = '';
var name = '';
var defaultCurrencyCode = '';
var defaultLanguageCode = '';
var domainName = '';
var i = 0;
final countI = json.sizeOfArray('payload');
while (i < countI) {
json.i = i;
id = json.stringOf('payload[i].marketplace.id');
countryCode = json.stringOf('payload[i].marketplace.countryCode');
name = json.stringOf('payload[i].marketplace.name');
defaultCurrencyCode = json.stringOf('payload[i].marketplace.defaultCurrencyCode');
defaultLanguageCode = json.stringOf('payload[i].marketplace.defaultLanguageCode');
domainName = json.stringOf('payload[i].marketplace.domainName');
json.boolOf('payload[i].participation.isParticipating');
json.boolOf('payload[i].participation.hasSuspendedListings');
i++;
}
print('Success!');
}