Rust
Rust
Create Restricted Data Token (RDT)
See more Amazon SP-API Examples
Returns a Restricted Data Token (RDT) for one or more restricted resources that you specify.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
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");
let rest = chilkat::Rest::new();
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
// The sandbox endpoint (sandbox.sellingpartnerapi-eu.amazon.com) fails.
// Use the production endpoint and see the note below.
if rest.connect("sellingpartnerapi-eu.amazon.com", port, b_tls, b_auto_reconnect).is_err() {
println!("{}", rest.last_error_text());
return;
}
let _ = rest.set_auth_aws(&auth_aws).is_ok();
// Load the previously obtained LWA access token.
// See Fetch SP-API LWA Access Token
let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/sp_api_lwa_token.json").is_err() {
println!("Failed to load LWA access token.");
return;
}
// Add the x-amz-access-token request header.
let lwa_token = json_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",
// "dataElements": ["buyerInfo", "shippingAddress"]
// }
// ]
// }
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
let json = chilkat::JsonObject::new();
let _ = json.update_string("restrictedResources[0].method", "GET");
let _ = json.update_string("restrictedResources[0].path", "/orders/v0/orders");
let _ = json.update_string("restrictedResources[0].dataElements[0]", "buyerInfo");
let _ = json.update_string("restrictedResources[0].dataElements[1]", "shippingAddress");
let sb_request = chilkat::StringBuilder::new();
let _ = json.emit_sb(&sb_request);
let sb_response = chilkat::StringBuilder::new();
let 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;
}
// ------------------------------------------------------------------------------------
// Note: Using the sandbox endpoint, such as sandbox.sellingpartnerapi-eu.amazon.com
// results in a response status code of 400 with the following error:
// [{"code":"InvalidInput","message":"Could not match input arguments"}]
// Getting a restricted data token seems to only work with the production endpoint.
// ------------------------------------------------------------------------------------
// Examine the response status.
let 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;
}
println!("{}", sb_response.get_as_string().unwrap_or_default());
// If successful, gets a JSON response such as the following:
// {
// "expiresIn": 3600,
// "restrictedDataToken": "Atz.sprdt|AYAB.....TQ="
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
let json_resp = chilkat::JsonObject::new();
let _ = json_resp.load_sb(&sb_response);
let expires_in = json_resp.int_of("expiresIn");
let restricted_data_token = json_resp.string_of("restrictedDataToken").unwrap_or_default();
// Save the RDT for subsequent use..
let _ = json_resp.write_file("qa_data/tokens/sp_api_rdt_token.json").is_ok();
println!("Success!");