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

Box: Refresh and Save Rotated OAuth Tokens

See more Box Examples

Refresh the user token saved by the authorization example. Box refresh tokens are single-use and expire after 60 days. Serialize refreshes for a token pair and save the replacement immediately; do not run concurrent refresh jobs.

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

Chilkat Rust Downloads

Rust

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

let _ = false;

let tokens = chilkat::JsonObject::new();
if tokens.load_file("qa_data/tokens/box_oauth.json").is_err() {
    println!("{}", tokens.last_error_text());
    return;
}

let oauth2 = chilkat::OAuth2::new();
oauth2.set_token_endpoint("https://api.box.com/oauth2/token");
oauth2.set_client_id("BOX_CLIENT_ID");
oauth2.set_client_secret("BOX_CLIENT_SECRET");
oauth2.set_refresh_token(&tokens.string_of("refresh_token").unwrap_or_default());
if oauth2.refresh_access_token().is_err() {
    println!("{}", oauth2.last_error_text());
    return;
}

if tokens.load(&oauth2.access_token_response()).is_err() {
    println!("{}", tokens.last_error_text());
    return;
}

// The previous refresh token is now invalid. Preserve this response even if
// your normal token-store write fails. Do not retry with the old refresh token.
if tokens.write_file("qa_data/tokens/box_oauth.json").is_err() {
    println!("{}", tokens.last_error_text());
    return;
}

println!("New access and refresh tokens saved. Do not log their values.");