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

Visual FoxPro
LOCAL lnSuccess
LOCAL loOauth
LOCAL lcUrl
LOCAL lnWaitedMs
LOCAL lnState
LOCAL loReply
LOCAL lcAccess
LOCAL lnLifetime
LOCAL lcRefresh
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL loNow

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0

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

*  The registered HTTPS callback forwards to this desktop's HTTP listener.
loOauth.AppCallbackUrl = "https://127.0.0.1:8182/"
loOauth.LocalHost = "127.0.0.1"
loOauth.ListenPort = 3017
lcUrl = loOauth.StartAuth()
IF (loOauth.LastMethodSuccess <> 1) THEN
    ? "Could not start authorization. Check the callback settings and free the listener port."
    RELEASE loOauth
    CANCEL
ENDIF

lnSuccess = loOauth.LaunchBrowser(lcUrl)
IF (lnSuccess <> 1) THEN
    loOauth.Cancel()
    ? "Could not open the system browser."
    RELEASE loOauth
    CANCEL
ENDIF

*  Allow time for sign-in, MFA, and account selection.
lnWaitedMs = 0
DO WHILE (lnWaitedMs < 300000) AND (loOauth.AuthFlowState < 3)
    loOauth.SleepMs(100)
    lnWaitedMs = lnWaitedMs + 100
ENDDO
lnState = loOauth.AuthFlowState
IF (lnState < 3) THEN
    loOauth.Cancel()
    ? "Authorization timed out. Check the HTTPS callback and try again."
    RELEASE loOauth
    CANCEL
ENDIF

IF (lnState = 4) THEN
    ? "Authorization was declined."
    RELEASE loOauth
    CANCEL
ENDIF

IF (lnState <> 3) THEN
    ? "Authorization failed. Check app approval, credentials, and the exact callback URL."
    RELEASE loOauth
    CANCEL
ENDIF

loReply = CreateObject('Chilkat.JsonObject')
lnSuccess = loReply.Load(loOauth.AccessTokenResponse)
IF (lnSuccess <> 1) THEN
    ? "Unexpected token response."
    RELEASE loOauth
    RELEASE loReply
    CANCEL
ENDIF

IF (loReply.HasMember("access_token") <> 1) THEN
    ? "The token response contained no access token."
    RELEASE loOauth
    RELEASE loReply
    CANCEL
ENDIF

lcAccess = loReply.StringOf("access_token")
lnLifetime = loReply.IntOf("expires_in")
IF ((lcAccess = "") OR (lnLifetime <= 0)) THEN
    ? "The token response lacks a usable access token or lifetime."
    RELEASE loOauth
    RELEASE loReply
    CANCEL
ENDIF

IF (loReply.HasMember("refresh_token") <> 1) THEN
    ? "No refresh token in the authorization response."
    RELEASE loOauth
    RELEASE loReply
    CANCEL
ENDIF

lcRefresh = loReply.StringOf("refresh_token")
IF (lcRefresh = "") THEN
    ? "The refresh token is empty."
    RELEASE loOauth
    RELEASE loReply
    CANCEL
ENDIF

loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","SchwabTrader")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","personal-desktop")
loTokens = CreateObject('Chilkat.JsonObject')
loTokens.UpdateString("refresh_token",lcRefresh)
loNow = CreateObject('Chilkat.CkDateTime')
loNow.SetFromCurrentSystemTime()
loTokens.UpdateString("authorized_at_utc",loNow.GetAsTimestamp(0))
loTokens.UpdateString("obtained_at_utc",loNow.GetAsTimestamp(0))
loTokens.UpdateString("access_token",lcAccess)
loTokens.UpdateInt("expires_in",lnLifetime)
lnSuccess = loVault.UpdateSecretJson(loEntry,loTokens)
IF (lnSuccess <> 1) THEN
    ? "Could not save the new credentials. Check the configured secret store."
    RELEASE loOauth
    RELEASE loReply
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loNow
    CANCEL
ENDIF

? "Saved credentials. Access token lifetime in seconds: " + STR(lnLifetime)

RELEASE loOauth
RELEASE loReply
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loNow