Rust Requires Chilkat v11.5.0+
Rust
Box: Authorize a User with OAuth 2.0
See more Box Examples
Interactive local-development example using a browser and a loopback callback. Register exactly http://localhost:3017/ in the Box User app. Run this in a trusted environment that can hold the client secret. A deployed web app must handle its registered HTTPS callback on the server.
See the Box API guide for setup and the complete workflow.
Chilkat Rust Downloads
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
let _ = false;
let oauth2 = chilkat::OAuth2::new();
oauth2.set_listen_port(3017);
oauth2.set_authorization_endpoint("https://account.box.com/api/oauth2/authorize");
oauth2.set_token_endpoint("https://api.box.com/oauth2/token");
oauth2.set_client_id("BOX_CLIENT_ID");
oauth2.set_client_secret("BOX_CLIENT_SECRET");
// Use Box's documented confidential-client authorization-code flow.
oauth2.set_code_challenge(false);
let Ok(url) = oauth2.start_auth() else {
println!("{}", oauth2.last_error_text());
return;
};
if oauth2.launch_browser(&url).is_err() {
println!("{}", oauth2.last_error_text());
return;
}
let mut elapsed_ms = 0;
while (elapsed_ms < 120000) && (oauth2.auth_flow_state() < 3) {
oauth2.sleep_ms(100);
elapsed_ms = elapsed_ms + 100;
}
if oauth2.auth_flow_state() < 3 {
let _ = oauth2.cancel();
println!("Authorization timed out.");
return;
}
if oauth2.auth_flow_state() != 3 {
println!("Authorization was denied or failed. State: {}", oauth2.auth_flow_state());
return;
}
// Retain both tokens in your application's protected token store.
// The variables below are deliberately not printed.
let access_token = oauth2.access_token();
let refresh_token = oauth2.refresh_token();
// For this local-development example only, save in a directory restricted to this user.
// Create qa_data/tokens before running; never put this file under a web root.
let tokens = chilkat::JsonObject::new();
if tokens.load(&oauth2.access_token_response()).is_err() {
println!("{}", tokens.last_error_text());
return;
}
if tokens.write_file("qa_data/tokens/box_oauth.json").is_err() {
println!("{}", tokens.last_error_text());
return;
}
println!("Authorization complete; token response saved in the protected local token file.");