Rust Requires Chilkat v11.0.0+
Rust
ShippingEasy.com Calculate Signature for API Authentication
See more HTTP Misc Examples
Demonstrates how to calculate the shippingeasy.com API signature for authenticating requests.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First, concatenate these into a plaintext string using the following order:
//
// Capitilized method of the request. E.g. "POST"
// The URI path
// The query parameters sorted alphabetically and concatenated together into a URL friendly format: param1=ABC¶m2=XYZ
// The request body as a string if one exists
// All parts are then concatenated together with an ampersand. The result resembles something like this:
//
// "POST&/partners/api/accounts&api_key=f9a7c8ebdfd34beaf260d9b0296c7059&api_timestamp=1401803554&{ ... request body ... }"
let sb_string_to_sign = chilkat::StringBuilder::new();
let http_verb = "POST".to_string();
let uri_path = "/partners/api/accounts".to_string();
let query_params_str = "api_key=YOUR_API_KEY&api_timestamp=UNIX_EPOCH_TIMESTAMP".to_string();
// Build the following JSON that will be the body of the request:
// {
// "account": {
// "first_name": "Coralie",
// "last_name": "Waelchi",
// "company_name": "Hegmann, Cremin and Bradtke",
// "email": "se_greg_6d477b1e59e8ff24abadfb59d3a2de3e@shippingeasy.com",
// "phone_number": "1-381-014-3358",
// "address": "2476 Flo Inlet",
// "address2": "",
// "state": "SC",
// "city": "North Dennis",
// "postal_code": "29805",
// "country": "USA",
// "password": "abc123",
// "subscription_plan_code": "starter"
// }
// }
let json = chilkat::JsonObject::new();
let _ = json.update_string("account.first_name", "Coralie");
let _ = json.update_string("account.last_name", "Waelchi");
let _ = json.update_string("account.company_name", "Hegmann, Cremin and Bradtke");
let _ = json.update_string("account.email", "se_greg_6d477b1e59e8ff24abadfb59d3a2de3e@shippingeasy.com");
let _ = json.update_string("account.phone_number", "1-381-014-3358");
let _ = json.update_string("account.address", "2476 Flo Inlet");
let _ = json.update_string("account.address2", "");
let _ = json.update_string("account.state", "SC");
let _ = json.update_string("account.city", "North Dennis");
let _ = json.update_string("account.postal_code", "29805");
let _ = json.update_string("account.country", "USA");
let _ = json.update_string("account.password", "abc123");
let _ = json.update_string("account.subscription_plan_code", "starter");
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
// First, let's get the current date/time in the Unix Epoch Timestamp format (which is just an integer)
let dt = chilkat::DateTime::new();
let _ = dt.set_from_current_system_time();
// Get the UTC time.
let b_local_time = false;
let unix_epoch_timestamp = dt.get_as_unix_time_str(b_local_time).unwrap_or_default();
// Build the string to sign:
let _ = sb_string_to_sign.append(&http_verb);
let _ = sb_string_to_sign.append("&");
let _ = sb_string_to_sign.append(&uri_path);
let _ = sb_string_to_sign.append("&");
let _ = sb_string_to_sign.append(&query_params_str);
let _ = sb_string_to_sign.append("&");
// Make sure to send the JSON body of a request in compact form..
json.set_emit_compact(true);
let _ = sb_string_to_sign.append(&json.emit().unwrap_or_default());
// Use your API key here:
let your_api_key = "f9a7c8ebdfd34beaf260d9b0296c7059".to_string();
let mut num_replaced = sb_string_to_sign.replace("YOUR_API_KEY", &your_api_key);
num_replaced = sb_string_to_sign.replace("UNIX_EPOCH_TIMESTAMP", &unix_epoch_timestamp);
// Do the HMAC-SHA256 with your API secret:
let your_api_secret = "ea210785fa4656af03c2e4ffcc2e7b5fc19f1fba577d137905cc97e74e1df53d".to_string();
let crypt = chilkat::Crypt2::new();
crypt.set_mac_algorithm("hmac");
crypt.set_encoding_mode("hexlower");
let _ = crypt.set_mac_key_string(&your_api_secret);
crypt.set_hash_algorithm("sha256");
let mut api_signature = crypt.mac_string_enc(&sb_string_to_sign.get_as_string().unwrap_or_default()).unwrap_or_default();
println!("api_signature: {}", api_signature);
// --------------------------------------------------------------------
// Here's an example showing how to use the signature in a request:
// Build a new string-to-sign and create a new api_signature for the actual request we'll be sending...
sb_string_to_sign.clear();
let _ = sb_string_to_sign.append("GET");
let _ = sb_string_to_sign.append("&");
let _ = sb_string_to_sign.append("/app.shippingeasy.com/api/orders");
let _ = sb_string_to_sign.append("&");
let _ = sb_string_to_sign.append(&query_params_str);
let _ = sb_string_to_sign.append("&");
// There is no body for a GET request.
api_signature = crypt.mac_string_enc(&sb_string_to_sign.get_as_string().unwrap_or_default()).unwrap_or_default();
let http = chilkat::Http::new();
let query_params = chilkat::JsonObject::new();
let _ = query_params.update_string("api_signature", &api_signature);
let _ = query_params.update_string("api_timestamp", &unix_epoch_timestamp);
let _ = query_params.update_string("api_key", &your_api_key);
let resp = chilkat::HttpResponse::new();
if http.http_params("GET", "https://app.shippingeasy.com/api/orders", &query_params, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("response status code = {}", resp.status_code());
println!("response body:");
println!("{}", resp.body_str());