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

Xbase++
LOCAL nSuccess
LOCAL oOauth
LOCAL cUrl
LOCAL nWaitedMs
LOCAL nState
LOCAL oReply
LOCAL cAccess
LOCAL nLifetime
LOCAL cRefresh
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL oNow

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0

oOauth := CreateObject("Chilkat.OAuth2")
oOauth:AuthorizationEndpoint := "https://api.schwabapi.com/v1/oauth/authorize"
oOauth:TokenEndpoint := "https://api.schwabapi.com/v1/oauth/token"
oOauth:ClientId := "APP_KEY"
oOauth:ClientSecret := "APP_SECRET"
oOauth:UseBasicAuth := 1
oOauth:CodeChallenge := 0

//  The registered HTTPS callback forwards to this desktop's HTTP listener.
oOauth:AppCallbackUrl := "https://127.0.0.1:8182/"
oOauth:LocalHost := "127.0.0.1"
oOauth:ListenPort := 3017
cUrl := oOauth:StartAuth()
IF (oOauth:LastMethodSuccess != 1)
    ? "Could not start authorization. Check the callback settings and free the listener port."
    oOauth:destroy()
    RETURN
ENDIF

nSuccess := oOauth:LaunchBrowser(cUrl)
IF (nSuccess != 1)
    oOauth:Cancel()
    ? "Could not open the system browser."
    oOauth:destroy()
    RETURN
ENDIF

//  Allow time for sign-in, MFA, and account selection.
nWaitedMs := 0
DO WHILE (nWaitedMs < 300000) .AND. (oOauth:AuthFlowState < 3)
    oOauth:SleepMs(100)
    nWaitedMs := nWaitedMs + 100
ENDDO
nState := oOauth:AuthFlowState
IF (nState < 3)
    oOauth:Cancel()
    ? "Authorization timed out. Check the HTTPS callback and try again."
    oOauth:destroy()
    RETURN
ENDIF

IF (nState == 4)
    ? "Authorization was declined."
    oOauth:destroy()
    RETURN
ENDIF

IF (nState != 3)
    ? "Authorization failed. Check app approval, credentials, and the exact callback URL."
    oOauth:destroy()
    RETURN
ENDIF

oReply := CreateObject("Chilkat.JsonObject")
nSuccess := oReply:Load(oOauth:AccessTokenResponse)
IF (nSuccess != 1)
    ? "Unexpected token response."
    oOauth:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:HasMember("access_token") != 1)
    ? "The token response contained no access token."
    oOauth:destroy()
    oReply:destroy()
    RETURN
ENDIF

cAccess := oReply:StringOf("access_token")
nLifetime := oReply:IntOf("expires_in")
IF ((cAccess == "") .OR. (nLifetime <= 0))
    ? "The token response lacks a usable access token or lifetime."
    oOauth:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:HasMember("refresh_token") != 1)
    ? "No refresh token in the authorization response."
    oOauth:destroy()
    oReply:destroy()
    RETURN
ENDIF

cRefresh := oReply:StringOf("refresh_token")
IF (cRefresh == "")
    ? "The refresh token is empty."
    oOauth:destroy()
    oReply:destroy()
    RETURN
ENDIF

oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "SchwabTrader")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "personal-desktop")
oTokens := CreateObject("Chilkat.JsonObject")
oTokens:UpdateString("refresh_token", cRefresh)
oNow := CreateObject("Chilkat.CkDateTime")
oNow:SetFromCurrentSystemTime()
oTokens:UpdateString("authorized_at_utc", oNow:GetAsTimestamp(0))
oTokens:UpdateString("obtained_at_utc", oNow:GetAsTimestamp(0))
oTokens:UpdateString("access_token", cAccess)
oTokens:UpdateInt("expires_in", nLifetime)
nSuccess := oVault:UpdateSecretJson(oEntry, oTokens)
IF (nSuccess != 1)
    ? "Could not save the new credentials. Check the configured secret store."
    oOauth:destroy()
    oReply:destroy()
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oNow:destroy()
    RETURN
ENDIF

? "Saved credentials. Access token lifetime in seconds: " + Str(nLifetime)

oOauth:destroy()
oReply:destroy()
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oNow:destroy()