Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.5.0+

Box: Get a Service Account Token with Client Credentials

See more Box Examples

Obtain a Box service-account token using the client credentials grant. Configure and authorize a Server app first. Load credentials from protected application configuration; do not ship a client secret in a public client.

See the Box API guide for setup and the complete workflow.

Chilkat Rust Downloads

Rust
let mut success = false;

// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.

success = false;

let curl = chilkat::HttpCurl::new();
curl.set_var("client_id", "BOX_CLIENT_ID");
curl.set_var("client_secret", "BOX_CLIENT_SECRET");
curl.set_var("enterprise_id", "BOX_ENTERPRISE_ID");
// enterprise identifies the app's service account. This is not an admin-user token.
curl.clear_target_output("*");
curl.clear_var("access_token");
curl.add_target_output("access_token", "access_token");
success = curl.do_your_thing("curl -X POST \"https://api.box.com/oauth2/token\" --data-urlencode \"grant_type=client_credentials\" --data-urlencode \"client_id={{client_id}}\" --data-urlencode \"client_secret={{client_secret}}\" --data-urlencode \"box_subject_type=enterprise\" --data-urlencode \"box_subject_id={{enterprise_id}}\"").is_ok();
if (!success) || (curl.status_code() != 200) {
    println!("Box request failed. HTTP status: {}", curl.status_code());
    println!("Failure reason: {}", curl.fail_reason());
    return;
}

if !curl.var_defined("!") {
    println!("Expected response fields are missing.");
    return;
}

// Keep this token in protected application memory; reacquire it when it expires.
// Do not print tokens or the complete token response.
println!("Service-account access token obtained.");
curl.clear_target_output("*");
let authorized_command1 = chilkat::StringBuilder::new();
let _ = authorized_command1.set_string("curl -X GET \"https://api.box.com/2.0/users/me?fields=id,name,login\" -H \"Authorization: Bearer {{access_token}}\"");
let header_replacement1 = authorized_command1.replace("{{access_token}}", &curl.get_var("access_token").unwrap_or_default());
success = curl.do_your_thing(&authorized_command1.get_as_string().unwrap_or_default()).is_ok();
if (!success) || (curl.status_code() != 200) {
    println!("Box request failed. HTTP status: {}", curl.status_code());
    println!("Failure reason: {}", curl.fail_reason());
    println!("{}", curl.response_body_str());
    return;
}

println!("{}", curl.response_body_str());