Sample code for 30+ languages & platforms
Rust

Banco Inter OAuth2 Client Credentials

Generate an OAuth2 access token needed to consume the Inter APIs.

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();

// First load the certificate and private key, and set as the HTTP object's client certificate.
let cert = chilkat::Cert::new();
if cert.load_from_file("<nome arquivo certificado>.crt").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

let bd_priv_key = chilkat::BinData::new();
if bd_priv_key.load_file("<nome arquivo chave privada>.key").is_err() {
    println!("Failed to load <nome");
    return;
}

let priv_key = chilkat::PrivateKey::new();
if priv_key.load_any_format(&bd_priv_key, "").is_err() {
    println!("{}", priv_key.last_error_text());
    return;
}

if cert.set_private_key(&priv_key).is_err() {
    println!("{}", cert.last_error_text());
    return;
}

if http.set_ssl_client_cert(&cert).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/oauth/v2/token");
req.set_content_type("application/x-www-form-urlencoded");
req.add_param("grant_type", "client_credentials");
// Requested scopes in OAuth2 are typically SPACE separated.
req.add_param("scope", "boleto-cobranca.read boleto-cobranca.write");
req.add_header("accept", "application/json");

let resp = chilkat::HttpResponse::new();
if http.http_req("https://cdpj.partners.bancointer.com.br/oauth/v2/token", &req, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let j_resp = chilkat::JsonObject::new();
let _ = resp.get_body_json(&j_resp);
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;
}

if j_resp.write_file("qa_data/tokens/banco_inter_client_credentials.json").is_err() {
    println!("Failed to save JSON access token file.");
    return;
}

println!("Success.");