Rust Requires Chilkat v11.0.0+
Rust
Calendar: Refresh Expired OAuth2 Access Token
See more Microsoft Calendar Examples
Refreshes an expired OAuth2 access token.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let json = chilkat::JsonObject::new();
if json.load_file("qa_data/tokens/msGraphCalendar.json").is_err() {
return;
}
let req = chilkat::HttpRequest::new();
req.add_param("grant_type", "refresh_token");
req.add_param("redirect_uri", "http://localhost:3017/");
req.add_param("client_id", "MICROSOFT-GRAPH-CLIENT-ID");
req.add_param("client_secret", "MICROSOFT-GRAPH-CLIENT-SECRET");
req.add_param("refresh_token", &json.string_of("refresh_token").unwrap_or_default());
req.add_param("scope", "openid profile offline_access user.readwrite calendars.readwrite files.readwrite");
let http = chilkat::Http::new();
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
let resp = chilkat::HttpResponse::new();
if http.http_req("https://login.microsoftonline.com/common/oauth2/v2.0/token", &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
// Load the JSON response.
let _ = json.load(&resp.body_str());
json.set_emit_compact(false);
// Show the JSON response.
println!("{}", json.emit().unwrap_or_default());
println!("Response status code: {}", resp.status_code());
// If the response status code is not 200, then it's an error.
if resp.status_code() != 200 {
return;
}
// Save the refreshed access token JSON to a file for future requests.
let fac = chilkat::FileAccess::new();
let _ = fac.write_entire_text_file("qa_data/tokens/msGraphCalendar.json", &json.emit().unwrap_or_default(), "utf-8", false);
println!("Success.");