Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

AzureWebsites OAuth2 Password Flow

See more OAuth2 Examples

Demonstrates how to do OAuth 2.0 password flow for azurewebsites.net.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();

let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/token");
req.set_content_type("application/x-www-form-urlencoded");
req.add_param("grant_type", "password");
req.add_param("username", "your_username");
req.add_param("password", "your_password");

let token_endpoint = "https://your_api.azurewebsites.net/token".to_string();

let resp = chilkat::HttpResponse::new();
if http.http_req(&token_endpoint, &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());

// Sample JSON response:

// {
//   "access_token": "NQGHn ... xTS",
//   "token_type": "bearer",
//   "expires_in": 1209599,
//   "userName": "your_username",
//   ".issued": "Mon, 27 Apr 2020 23:49:35 GMT",
//   ".expires": "Mon, 11 May 2020 23:49:35 GMT"
// }

let mut 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;
}

// ----------------------------------
// Use the OAuth2 token in a request.
// For example...

let sb_xml = chilkat::StringBuilder::new();
if sb_xml.load_file("c:/someDir/someXmlFile.xml", "utf-8").is_err() {
    println!("Failed to load the XML file.");
    return;
}

// Get the OAuth2 token and use it for authentication
http.set_auth_token(&j_resp.string_of("token").unwrap_or_default());

let dest_url = "https://your_api.azurewebsites.net/destinationUrl".to_string();
if http.http_sb("POST", &dest_url, &sb_xml, "utf-8", "application/xml", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

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;
}

// Examine the response body
println!("{}", resp.body_str());