Sample code for 30+ languages & platforms
Rust

citi Developer OAuth2 Client Credentials Grant

See more OAuth2 Examples

Get access token for your application credentials. You can use this for citi APIs which do not require customer credential verification and consent (e.g. Onboarding).

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

// Implements the following CURL command:

// curl --request POST \
//   --url https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb \
//   --header 'accept: application/json' \
//   --user client-id:client-secret \
//   --header 'content-type: application/x-www-form-urlencoded' \
//   --data 'grant_type=client_credentials&scope=%2Fapi'

http.set_login("client-id");
http.set_password("client-secret");

let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/gcb/api/clientCredentials/oauth2/token/us/gcb");
req.set_content_type("application/x-www-form-urlencoded");
req.add_param("grant_type", "client_credentials");
req.add_param("scope", "/api");
req.add_header("accept", "application/json");

let resp = chilkat::HttpResponse::new();
if http.http_req("https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb", &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;
}

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

//  Sample JSON response:
//  (Sample code for parsing the JSON response is shown below)

//  {
//    "token_type": "bearer",
//    "access_token": "AAIkMjdh ... 3fsWb7zJ0s",
//    "expires_in": 1800,
//    "consented_on": 1584817860,
//    "scope": "/api"
//  }

//  Sample code for parsing the JSON response...
//  Use the following online tool to generate parsing code from sample JSON:
//  Generate Parsing Code from JSON

let token_type = j_resp.string_of("token_type").unwrap_or_default();
let access_token = j_resp.string_of("access_token").unwrap_or_default();
let expires_in = j_resp.int_of("expires_in");
let consented_on = j_resp.int_of("consented_on");
let scope = j_resp.string_of("scope").unwrap_or_default();