Sample code for 30+ languages & platforms
Rust Requires Chilkat v10.1.2+

Schwab OAuth 2.0: Authorize a desktop application

See more Schwab Examples

Use a Schwab Trader API app approved for your own desktop application. Register https://127.0.0.1:8182/ as its callback and enter exactly that URL in the code, including the trailing slash. Load your app key and secret from protected configuration. The browser handles Schwab sign-in, account selection, and consent.

Schwab requires HTTPS. Chilkat’s built-in OAuth listener speaks HTTP. For this example, run a loopback HTTPS reverse proxy on port 8182 that forwards the original path and query string to http://127.0.0.1:3017. Use a certificate trusted by the browser for 127.0.0.1. For example, start Caddy with the following Caddyfile and trust its local CA on the desktop:

https://127.0.0.1:8182 {
    bind 127.0.0.1
    tls internal
    reverse_proxy 127.0.0.1:3017
}

Keep callback query strings out of proxy logs. If you already use an application-controlled HTTPS callback relay, register its exact URL instead and forward the authorization response to the initiating desktop. After consent, use Refresh a Schwab access token during the seven-day refresh-token lifetime.

These examples use the current user’s Windows Credential Manager or Apple Keychain. On Linux or Android, configure a persistent Chilkat Secrets backend for both examples before running them. The local manager on those platforms uses process memory. Use the same secret name for the same app and Schwab authorization. Give each separate authorization its own name.

Schwab Trader API OAuth guide · Register a Schwab app

Chilkat Rust Downloads

Rust

// Assumes Chilkat is unlocked. See Global Unlock Sample.
let _ = false;

let oauth = chilkat::OAuth2::new();
oauth.set_authorization_endpoint("https://api.schwabapi.com/v1/oauth/authorize");
oauth.set_token_endpoint("https://api.schwabapi.com/v1/oauth/token");
oauth.set_client_id("APP_KEY");
oauth.set_client_secret("APP_SECRET");
oauth.set_use_basic_auth(true);
oauth.set_code_challenge(false);

// The registered HTTPS callback forwards to this desktop's HTTP listener.
oauth.set_app_callback_url("https://127.0.0.1:8182/");
oauth.set_local_host("127.0.0.1");
oauth.set_listen_port(3017);
let Ok(url) = oauth.start_auth() else {
    println!("Could not start authorization. Check the callback settings and free the listener port.");
    return;
};

if oauth.launch_browser(&url).is_err() {
    let _ = oauth.cancel();
    println!("Could not open the system browser.");
    return;
}

// Allow time for sign-in, MFA, and account selection.
let mut waited_ms = 0;
while (waited_ms < 300000) && (oauth.auth_flow_state() < 3) {
    oauth.sleep_ms(100);
    waited_ms = waited_ms + 100;
}

let state = oauth.auth_flow_state();
if state < 3 {
    let _ = oauth.cancel();
    println!("Authorization timed out. Check the HTTPS callback and try again.");
    return;
}

if state == 4 {
    println!("Authorization was declined.");
    return;
}

if state != 3 {
    println!("Authorization failed. Check app approval, credentials, and the exact callback URL.");
    return;
}

let reply = chilkat::JsonObject::new();
if reply.load(&oauth.access_token_response()).is_err() {
    println!("Unexpected token response.");
    return;
}

if !reply.has_member("access_token") {
    println!("The token response contained no access token.");
    return;
}

let access = reply.string_of("access_token").unwrap_or_default();
let lifetime = reply.int_of("expires_in");
if (access == "") || (lifetime <= 0) {
    println!("The token response lacks a usable access token or lifetime.");
    return;
}

if !reply.has_member("refresh_token") {
    println!("No refresh token in the authorization response.");
    return;
}

let refresh = reply.string_of("refresh_token").unwrap_or_default();
if refresh == "" {
    println!("The refresh token is empty.");
    return;
}

let vault = chilkat::Secrets::new();
vault.set_location("local_manager");
vault.set_auto_split(true);
let entry = chilkat::JsonObject::new();
let _ = entry.update_string("appName", "SchwabTrader");
let _ = entry.update_string("service", "OAuth2");
let _ = entry.update_string("username", "personal-desktop");
let tokens = chilkat::JsonObject::new();
let _ = tokens.update_string("refresh_token", &refresh);
let now = chilkat::DateTime::new();
let _ = now.set_from_current_system_time();
let _ = tokens.update_string("authorized_at_utc", &now.get_as_timestamp(false).unwrap_or_default());
let _ = tokens.update_string("obtained_at_utc", &now.get_as_timestamp(false).unwrap_or_default());
let _ = tokens.update_string("access_token", &access);
let _ = tokens.update_int("expires_in", lifetime);
if vault.update_secret_json(&entry, &tokens).is_err() {
    println!("Could not save the new credentials. Check the configured secret store.");
    return;
}

println!("Saved credentials. Access token lifetime in seconds: {}", lifetime);