Sample code for 30+ languages & platforms
Rust

SugarCRM Authenticate

See more SugarCRM Examples

Demonstrates how to authenticate to the SugarCRM REST v10 API. This is how an OAuth2 access token is obtained.

Chilkat Rust Downloads

Rust

let rest = chilkat::Rest::new();

if rest.connect("your.site.domain", 443, true, true).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let _ = rest.add_header("Cache-Control", "no-cache");

// The following code creates the JSON request body.
// The JSON created by this code is shown below.
let json_req = chilkat::JsonObject::new();
let _ = json_req.update_string("grant_type", "password");
let _ = json_req.update_string("client_id", "sugar");
let _ = json_req.update_string("client_secret", "CLIENT_SECRET");
let _ = json_req.update_string("username", "admin");
let _ = json_req.update_string("password", "password");
let _ = json_req.update_string("platform", "custom_api");

// The JSON request body created by the above code:

// {
//   "grant_type": "password",
//   "client_id": "sugar",
//   "client_secret": "CLIENT_SECRET",
//   "username": "admin",
//   "password": "password",
//   "platform": "custom_api"
// }

let sb_req = chilkat::StringBuilder::new();
let _ = json_req.emit_sb(&sb_req);

let _ = rest.add_header("Content-Type", "application/json");

let sb_json = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/rest/v10/oauth2/token", &sb_req, &sb_json).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

if rest.response_status_code() != 200 {
    println!("Received error response code: {}", rest.response_status_code());
    println!("Response body:");
    println!("{}", sb_json.get_as_string().unwrap_or_default());
    return;
}

let json = chilkat::JsonObject::new();
let _ = json.load_sb(&sb_json);

// The following code parses the JSON response.
// A sample JSON response is shown below the sample code.

let access_token = json.string_of("access_token").unwrap_or_default();
let expires_in = json.int_of("expires_in");
let token_type = json.string_of("token_type").unwrap_or_default();
let scope = json.is_null_of("scope");
let refresh_token = json.string_of("refresh_token").unwrap_or_default();
let refresh_expires_in = json.int_of("refresh_expires_in");
let download_token = json.string_of("download_token").unwrap_or_default();

// A sample JSON response body that is parsed by the above code:

// {
//   "access_token": "c6d495c9-bb25-81d2-5f81-533ef6479f9b",
//   "expires_in": 3600,
//   "token_type": "bearer",
//   "scope": null,
//   "refresh_token": "cbc40e67-12bc-4b56-a1d9-533ef62f2601",
//   "refresh_expires_in": 1209600,
//   "download_token": "cc5d1a9f-6627-3349-96e5-533ef6b1a493"
// }

println!("Example Completed.");