Sample code for 30+ languages & platforms
Rust

Moody's REST API - Get OAuth2 Token

See more Moody's Examples

Demonstrates how to get an OAuth2 access token for the Moody's REST API.

Chilkat Rust Downloads

Rust

// This example assumes 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.add_param("grant_type", "password");
req.add_param("scope", "api/ratings api/addin rest");
req.add_param("username", "my_username");
req.add_param("password", "my_password");
// I have no idea of where to get the client_id or client_secret.
// When you create a Moody's App, it only provides an "API Key".
req.add_param("client_id", "my_client_id");
req.add_param("client_secret", "my_client_secret");

req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");

let resp = chilkat::HttpResponse::new();
if http.http_req("https://api.moodys.com/OAuth/Token", &req, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

println!("status code = {}", resp.status_code());
let response_body = resp.body_str();
println!("{}", response_body);

// Save the JSON to a file for future requests.
if resp.status_code() == 200 {
    let fac = chilkat::FileAccess::new();
    let _ = fac.write_entire_text_file("qa_data/tokens/moodys.json", &resp.body_str(), "utf-8", false);
}