Rust
Rust
UPS OAuth2 Client Credentials
See more UPS Examples
Get an OAuth2 access token for the UPS REST API using the client credentials flow (no interactivity with a web browser required).Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// Implements the following CURL command:
// curl -i -X POST \
// -u 2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt:nerf254578uh8rgt7y3h57358ouyth387h8h53h6yyh80hh578per9y7u5ruyuy4 \
// https://wwwcie.ups.com/security/v1/oauth/token \
// -H 'Content-Type: application/x-www-form-urlencoded' \
// -H 'x-merchant-id: 7B3027' \
// -d grant_type=client_credentials
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
http.set_basic_auth(true);
http.set_login("2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt");
http.set_password("nerf254578uh8rgt7y3h57358ouyth387h8h53h6yyh80hh578per9y7u5ruyuy4");
let req = chilkat::HttpRequest::new();
req.add_param("grant_type", "client_credentials");
req.add_header("x-merchant-id", "7B3027");
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
let resp = chilkat::HttpResponse::new();
if http.http_req("https://wwwcie.ups.com/security/v1/oauth/token", &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());
let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", resp.header());
println!("Failed.");
return;
}
// Save the OAuth2 access token for other examples to use.
let _ = j_resp.write_file("qa_data/tokens/ups_oauth2_token.json");
// If successful, the OAuth2 access token JSON looks like this:
// {
// "token_type": "Bearer",
// "issued_at": "1686911985606",
// "client_id": "2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt",
// "access_token": "eyJraW......R2sbqrY",
// "expires_in": "14399",
// "status": "approved"
// }
let token_type = j_resp.string_of("token_type").unwrap_or_default();
let issued_at = j_resp.string_of("issued_at").unwrap_or_default();
let client_id = j_resp.string_of("client_id").unwrap_or_default();
let access_token = j_resp.string_of("access_token").unwrap_or_default();
let expires_in = j_resp.string_of("expires_in").unwrap_or_default();
let status = j_resp.string_of("status").unwrap_or_default();