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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Oauth
string ls_Url
long li_WaitedMs
long li_State
oleobject loo_Reply
string ls_Access
long li_Lifetime
string ls_Refresh
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
oleobject loo_Now

li_Success = 0

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

loo_Oauth = create oleobject
li_rc = loo_Oauth.ConnectToNewObject("Chilkat.OAuth2")
if li_rc < 0 then
    destroy loo_Oauth
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Oauth.AuthorizationEndpoint = "https://api.schwabapi.com/v1/oauth/authorize"
loo_Oauth.TokenEndpoint = "https://api.schwabapi.com/v1/oauth/token"
loo_Oauth.ClientId = "APP_KEY"
loo_Oauth.ClientSecret = "APP_SECRET"
loo_Oauth.UseBasicAuth = 1
loo_Oauth.CodeChallenge = 0

//  The registered HTTPS callback forwards to this desktop's HTTP listener.
loo_Oauth.AppCallbackUrl = "https://127.0.0.1:8182/"
loo_Oauth.LocalHost = "127.0.0.1"
loo_Oauth.ListenPort = 3017
ls_Url = loo_Oauth.StartAuth()
if loo_Oauth.LastMethodSuccess <> 1 then
    Write-Debug "Could not start authorization. Check the callback settings and free the listener port."
    destroy loo_Oauth
    return
end if

li_Success = loo_Oauth.LaunchBrowser(ls_Url)
if li_Success <> 1 then
    loo_Oauth.Cancel()
    Write-Debug "Could not open the system browser."
    destroy loo_Oauth
    return
end if

//  Allow time for sign-in, MFA, and account selection.
li_WaitedMs = 0
do while (li_WaitedMs < 300000) AND (loo_Oauth.AuthFlowState < 3)
    loo_Oauth.SleepMs(100)
    li_WaitedMs = li_WaitedMs + 100
loop
li_State = loo_Oauth.AuthFlowState
if li_State < 3 then
    loo_Oauth.Cancel()
    Write-Debug "Authorization timed out. Check the HTTPS callback and try again."
    destroy loo_Oauth
    return
end if

if li_State = 4 then
    Write-Debug "Authorization was declined."
    destroy loo_Oauth
    return
end if

if li_State <> 3 then
    Write-Debug "Authorization failed. Check app approval, credentials, and the exact callback URL."
    destroy loo_Oauth
    return
end if

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Reply.Load(loo_Oauth.AccessTokenResponse)
if li_Success <> 1 then
    Write-Debug "Unexpected token response."
    destroy loo_Oauth
    destroy loo_Reply
    return
end if

if loo_Reply.HasMember("access_token") <> 1 then
    Write-Debug "The token response contained no access token."
    destroy loo_Oauth
    destroy loo_Reply
    return
end if

ls_Access = loo_Reply.StringOf("access_token")
li_Lifetime = loo_Reply.IntOf("expires_in")
if (ls_Access = "") OR (li_Lifetime <= 0) then
    Write-Debug "The token response lacks a usable access token or lifetime."
    destroy loo_Oauth
    destroy loo_Reply
    return
end if

if loo_Reply.HasMember("refresh_token") <> 1 then
    Write-Debug "No refresh token in the authorization response."
    destroy loo_Oauth
    destroy loo_Reply
    return
end if

ls_Refresh = loo_Reply.StringOf("refresh_token")
if ls_Refresh = "" then
    Write-Debug "The refresh token is empty."
    destroy loo_Oauth
    destroy loo_Reply
    return
end if

loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")

loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")

loo_Entry.UpdateString("appName","SchwabTrader")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","personal-desktop")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")

loo_Tokens.UpdateString("refresh_token",ls_Refresh)
loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")

loo_Now.SetFromCurrentSystemTime()
loo_Tokens.UpdateString("authorized_at_utc",loo_Now.GetAsTimestamp(0))
loo_Tokens.UpdateString("obtained_at_utc",loo_Now.GetAsTimestamp(0))
loo_Tokens.UpdateString("access_token",ls_Access)
loo_Tokens.UpdateInt("expires_in",li_Lifetime)
li_Success = loo_Vault.UpdateSecretJson(loo_Entry,loo_Tokens)
if li_Success <> 1 then
    Write-Debug "Could not save the new credentials. Check the configured secret store."
    destroy loo_Oauth
    destroy loo_Reply
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Now
    return
end if

Write-Debug "Saved credentials. Access token lifetime in seconds: " + string(li_Lifetime)


destroy loo_Oauth
destroy loo_Reply
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Now