Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0

set oauth [new_CkOAuth2]

CkOAuth2_put_AuthorizationEndpoint $oauth "https://api.schwabapi.com/v1/oauth/authorize"
CkOAuth2_put_TokenEndpoint $oauth "https://api.schwabapi.com/v1/oauth/token"
CkOAuth2_put_ClientId $oauth "APP_KEY"
CkOAuth2_put_ClientSecret $oauth "APP_SECRET"
CkOAuth2_put_UseBasicAuth $oauth 1
CkOAuth2_put_CodeChallenge $oauth 0

#  The registered HTTPS callback forwards to this desktop's HTTP listener.
CkOAuth2_put_AppCallbackUrl $oauth "https://127.0.0.1:8182/"
CkOAuth2_put_LocalHost $oauth "127.0.0.1"
CkOAuth2_put_ListenPort $oauth 3017
set url [CkOAuth2_startAuth $oauth]
if {[CkOAuth2_get_LastMethodSuccess $oauth] != 1} then {
    puts "Could not start authorization. Check the callback settings and free the listener port."
    delete_CkOAuth2 $oauth
    exit
}

set success [CkOAuth2_LaunchBrowser $oauth $url]
if {$success != 1} then {
    CkOAuth2_Cancel $oauth
    puts "Could not open the system browser."
    delete_CkOAuth2 $oauth
    exit
}

#  Allow time for sign-in, MFA, and account selection.
set waitedMs 0
while {expr [$waitedMs < 300000]  &&  [[CkOAuth2_get_AuthFlowState $oauth] < 3]} {
    CkOAuth2_SleepMs $oauth 100
    set waitedMs [expr $waitedMs + 100]
}
set state [CkOAuth2_get_AuthFlowState $oauth]
if {$state < 3} then {
    CkOAuth2_Cancel $oauth
    puts "Authorization timed out. Check the HTTPS callback and try again."
    delete_CkOAuth2 $oauth
    exit
}

if {$state == 4} then {
    puts "Authorization was declined."
    delete_CkOAuth2 $oauth
    exit
}

if {$state != 3} then {
    puts "Authorization failed. Check app approval, credentials, and the exact callback URL."
    delete_CkOAuth2 $oauth
    exit
}

set reply [new_CkJsonObject]

set success [CkJsonObject_Load $reply [CkOAuth2_accessTokenResponse $oauth]]
if {$success != 1} then {
    puts "Unexpected token response."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $reply
    exit
}

if {[CkJsonObject_HasMember $reply "access_token"] != 1} then {
    puts "The token response contained no access token."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $reply
    exit
}

set access [CkJsonObject_stringOf $reply "access_token"]
set lifetime [CkJsonObject_IntOf $reply "expires_in"]
if {expr [$access == ""]  ||  [$lifetime <= 0]} then {
    puts "The token response lacks a usable access token or lifetime."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $reply
    exit
}

if {[CkJsonObject_HasMember $reply "refresh_token"] != 1} then {
    puts "No refresh token in the authorization response."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $reply
    exit
}

set refresh [CkJsonObject_stringOf $reply "refresh_token"]
if {$refresh == ""} then {
    puts "The refresh token is empty."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $reply
    exit
}

set vault [new_CkSecrets]

CkSecrets_put_Location $vault "local_manager"
CkSecrets_put_AutoSplit $vault 1
set entry [new_CkJsonObject]

CkJsonObject_UpdateString $entry "appName" "SchwabTrader"
CkJsonObject_UpdateString $entry "service" "OAuth2"
CkJsonObject_UpdateString $entry "username" "personal-desktop"
set tokens [new_CkJsonObject]

CkJsonObject_UpdateString $tokens "refresh_token" $refresh
set now [new_CkDateTime]

CkDateTime_SetFromCurrentSystemTime $now
CkJsonObject_UpdateString $tokens "authorized_at_utc" [CkDateTime_getAsTimestamp $now 0]
CkJsonObject_UpdateString $tokens "obtained_at_utc" [CkDateTime_getAsTimestamp $now 0]
CkJsonObject_UpdateString $tokens "access_token" $access
CkJsonObject_UpdateInt $tokens "expires_in" $lifetime
set success [CkSecrets_UpdateSecretJson $vault $entry $tokens]
if {$success != 1} then {
    puts "Could not save the new credentials. Check the configured secret store."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $reply
    delete_CkSecrets $vault
    delete_CkJsonObject $entry
    delete_CkJsonObject $tokens
    delete_CkDateTime $now
    exit
}

puts "Saved credentials. Access token lifetime in seconds: $lifetime"

delete_CkOAuth2 $oauth
delete_CkJsonObject $reply
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkDateTime $now