Lua
Lua
Amazon SP-API Sellers Get Marketplace Participations
See more Amazon SP-API Examples
Demonstrates Amazon SP-API Sellers API -- get marketplace participations.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local authAws = chilkat.AuthAws.new()
authAws.access_key = "AWS_ACCESS_KEY"
authAws.secret_key = "AWS_SECRET_KEY"
authAws.service_name = "execute-api"
-- Use the region that is correct for your needs.
authAws.region = "eu-west-1"
local rest = chilkat.Rest.new()
local bTls = true
local port = 443
local bAutoReconnect = true
-- Make sure to use the correct domain.
-- In this example, we are using "sandbox.sellingpartnerapi-eu.amazon.com"
local success = rest:connect("sandbox.sellingpartnerapi-eu.amazon.com", port, bTls, bAutoReconnect)
if not success then
print(rest.last_error_text)
return
end
success = rest:set_auth_aws(authAws)
-- Load the previously obtained LWA access token.
-- See Fetch SP-API LWA Access Token
local jsonToken = chilkat.JsonObject.new()
success = jsonToken:load_file("qa_data/tokens/sp_api_lwa_token.json")
if not success then
print("Failed to load LWA access token.")
return
end
-- Add the x-amz-access-token request header.
local lwa_token = jsonToken:string_of("access_token")
rest:clear_all_headers()
rest:add_header("x-amz-access-token", lwa_token)
local sbResponse = chilkat.StringBuilder.new()
local uri = "/sellers/v1/marketplaceParticipations"
success = rest:full_request_no_body_sb("GET", uri, sbResponse)
if not success then
print(rest.last_error_text)
return
end
-- Examine the response status.
local statusCode = rest.response_status_code
if statusCode ~= 200 then
print("Response status text: " .. rest.response_status_text)
print("Response body: ")
print(sbResponse:get_as_string())
print("Failed.")
return
end
print(sbResponse:get_as_string())
-- 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
local json = chilkat.JsonObject.new()
json:load_sb(sbResponse)
local Id = ""
local CountryCode = ""
local Name = ""
local DefaultCurrencyCode = ""
local DefaultLanguageCode = ""
local DomainName = ""
local IsParticipating = false
local HasSuspendedListings = false
local i = 0
local count_i = json:size_of_array("payload")
while i < count_i do
json.i = i
Id = json:string_of("payload[i].marketplace.id")
CountryCode = json:string_of("payload[i].marketplace.countryCode")
Name = json:string_of("payload[i].marketplace.name")
DefaultCurrencyCode = json:string_of("payload[i].marketplace.defaultCurrencyCode")
DefaultLanguageCode = json:string_of("payload[i].marketplace.defaultLanguageCode")
DomainName = json:string_of("payload[i].marketplace.domainName")
IsParticipating = json:bool_of("payload[i].participation.isParticipating")
HasSuspendedListings = json:bool_of("payload[i].participation.hasSuspendedListings")
i = i + 1
end
print("Success!")