Rust
Rust
Amazon SP-API Get Order Items
See more Amazon SP-API Examples
Returns detailed order item information for the order that you specify.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Gets detailed order item information for a specified order ID
// The order ID is something like "902-1845936-5435065" and it is the AmazonOrderId returned in the JSON when getting the list of orders. For example:
// {
// "payload": {
// "CreatedBefore": "1.569521782042E9",
// "Orders": [
// {
// "AmazonOrderId": "902-1845936-5435065",
// "PurchaseDate": "1970-01-19T03:58:30Z",
// ...
// However, when using the sandbox, instead use the explicit keyword TEST_CASE_200
let order_id = "TEST_CASE_200".to_string();
let auth_aws = chilkat::AuthAws::new();
auth_aws.set_access_key("AWS_ACCESS_KEY");
auth_aws.set_secret_key("AWS_SECRET_KEY");
auth_aws.set_service_name("execute-api");
// Use the region that is correct for your needs.
auth_aws.set_region("eu-west-1");
// First get a restricted data token for the given order ID.
// This requires an LWA access token which cannot be more than 1 hour old.
// See Fetch SP-API LWA Access Token
let json_lwa_token = chilkat::JsonObject::new();
if json_lwa_token.load_file("qa_data/tokens/sp_api_lwa_token.json").is_err() {
println!("Failed to load LWA access token.");
return;
}
// Must use the non-sandbox domain for getting the RDT.
let rest = chilkat::Rest::new();
if rest.connect("sellingpartnerapi-eu.amazon.com", 443, true, true).is_err() {
println!("{}", rest.last_error_text());
return;
}
let _ = rest.set_auth_aws(&auth_aws).is_ok();
// Add the x-amz-access-token request header.
let lwa_token = json_lwa_token.string_of("access_token").unwrap_or_default();
let _ = rest.clear_all_headers();
let _ = rest.add_header("x-amz-access-token", &lwa_token);
// We're going to send a POST with the following JSON body:
// {
// "restrictedResources": [
// {
// "method": "GET",
// "path": "/orders/v0/orders/{orderId}/orderItems",
// "dataElements": ["buyerInfo"]
// }
// ]
// }
let sb_path = chilkat::StringBuilder::new();
let _ = sb_path.append("/orders/v0/orders/");
let _ = sb_path.append(&order_id);
let _ = sb_path.append("/orderItems");
let json_rc = chilkat::JsonObject::new();
let _ = json_rc.update_string("restrictedResources[0].method", "GET");
let _ = json_rc.update_string("restrictedResources[0].path", &sb_path.get_as_string().unwrap_or_default());
let _ = json_rc.update_string("restrictedResources[0].dataElements[0]", "buyerInfo");
let sb_request = chilkat::StringBuilder::new();
let _ = json_rc.emit_sb(&sb_request);
let sb_response = chilkat::StringBuilder::new();
let mut uri = "/tokens/2021-03-01/restrictedDataToken".to_string();
if rest.full_request_sb("POST", &uri, &sb_request, &sb_response).is_err() {
println!("{}", rest.last_error_text());
return;
}
// Examine the response status.
let mut status_code = rest.response_status_code();
if status_code != 200 {
println!("Response status code: {}", status_code);
println!("Response status text: {}", rest.response_status_text());
println!("Response body: ");
println!("{}", sb_response.get_as_string().unwrap_or_default());
println!("Failed.");
return;
}
// Get the restricted data token.
let json_resp = chilkat::JsonObject::new();
let _ = json_resp.load_sb(&sb_response);
let restricted_data_token = json_resp.string_of("restrictedDataToken").unwrap_or_default();
println!("Restricted Data Token: {}", restricted_data_token);
// ------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------
// Now that we have the RDT, we can use it to get information about the order.
//
// Yes, the SP-API is horribly tedious and painful. You must use an RDT specifically tailored to each request requiring an RDT,
// the RDT must not be over an hour old, and if you need to get a new RDT you must get it using an LWA token that itself is not
// more than an hour old. If the LWA is more than an hour old, you must get a new one. Ughhh!!!!!
// ------------------------------------------------------------------------------------------------------------
// Disconnect from the non-sandbox domain. This example will use the sandbox.
// (The RDT was obtained using the non-sandbox domain. For some reason, the sandbox domain does not work for getting the RDT.)
let _ = rest.disconnect(100);
if rest.connect("sandbox.sellingpartnerapi-eu.amazon.com", 443, true, true).is_err() {
println!("{}", rest.last_error_text());
return;
}
let _ = rest.set_auth_aws(&auth_aws).is_ok();
let _ = rest.clear_all_headers();
let _ = rest.add_header("x-amz-access-token", &restricted_data_token);
let _ = rest.clear_all_query_params();
let _ = rest.add_query_param("MarketplaceIds", "ATVPDKIKX0DER");
let _ = rest.clear_all_path_params();
let _ = rest.add_path_param("{orderId}", &order_id);
uri = "/orders/v0/orders/{orderId}/orderItems".to_string();
if rest.full_request_no_body_sb("GET", &uri, &sb_response).is_err() {
println!("{}", rest.last_error_text());
return;
}
// Examine the response status.
status_code = rest.response_status_code();
if status_code != 200 {
println!("Response status text: {}", rest.response_status_text());
println!("Response body: ");
println!("{}", sb_response.get_as_string().unwrap_or_default());
println!("Failed.");
return;
}
// If successful, gets a JSON response such as the following:
// {
// "payload": {
// "AmazonOrderId": "902-1845936-5435065",
// "OrderItems": [
// {
// "ASIN": "B00551Q3CS",
// "OrderItemId": "05015851154158",
// "SellerSKU": "NABetaASINB00551Q3CS",
// "Title": "B00551Q3CS [Card Book]",
// "QuantityOrdered": 1,
// "QuantityShipped": 0,
// "ProductInfo": {
// "NumberOfItems": 1
// },
// "ItemPrice": {
// "CurrencyCode": "USD",
// "Amount": "10.00"
// },
// "ItemTax": {
// "CurrencyCode": "USD",
// "Amount": "1.01"
// },
// "PromotionDiscount": {
// "CurrencyCode": "USD",
// "Amount": "0.00"
// },
// "IsGift": false,
// "ConditionId": "New",
// "ConditionSubtypeId": "New",
// "IsTransparency": false,
// "SerialNumberRequired": false,
// "IossNumber": "",
// "DeemedResellerCategory": "IOSS",
// "StoreChainStoreId": "ISPU_StoreId",
// "BuyerRequestedCancel": {
// "IsBuyerRequestedCancel": true,
// "BuyerCancelReason": "Found cheaper somewhere else."
// }
// }
// ]
// }
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
let json = chilkat::JsonObject::new();
let _ = json.load_sb(&sb_response);
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
let amazon_order_id = json.string_of("payload.AmazonOrderId").unwrap_or_default();
let mut i = 0;
let count_i = json.size_of_array("payload.OrderItems");
while i < count_i {
json.set_i(i);
let _ = json.string_of("payload.OrderItems[i].ASIN").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].OrderItemId").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].SellerSKU").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].Title").unwrap_or_default();
let _ = json.int_of("payload.OrderItems[i].QuantityOrdered");
let _ = json.int_of("payload.OrderItems[i].QuantityShipped");
let _ = json.int_of("payload.OrderItems[i].ProductInfo.NumberOfItems");
let _ = json.string_of("payload.OrderItems[i].ItemPrice.CurrencyCode").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].ItemPrice.Amount").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].ItemTax.CurrencyCode").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].ItemTax.Amount").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].PromotionDiscount.CurrencyCode").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].PromotionDiscount.Amount").unwrap_or_default();
let _ = json.bool_of("payload.OrderItems[i].IsGift");
let _ = json.string_of("payload.OrderItems[i].ConditionId").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].ConditionSubtypeId").unwrap_or_default();
let _ = json.bool_of("payload.OrderItems[i].IsTransparency");
let _ = json.bool_of("payload.OrderItems[i].SerialNumberRequired");
let _ = json.string_of("payload.OrderItems[i].IossNumber").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].DeemedResellerCategory").unwrap_or_default();
let _ = json.string_of("payload.OrderItems[i].StoreChainStoreId").unwrap_or_default();
let _ = json.bool_of("payload.OrderItems[i].BuyerRequestedCancel.IsBuyerRequestedCancel");
let _ = json.string_of("payload.OrderItems[i].BuyerRequestedCancel.BuyerCancelReason").unwrap_or_default();
i = i + 1;
}
println!("Success!");