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 Ruby Downloads
require 'chilkat'
success = false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
oauth = Chilkat::CkOAuth2.new()
oauth.put_AuthorizationEndpoint("https://api.schwabapi.com/v1/oauth/authorize")
oauth.put_TokenEndpoint("https://api.schwabapi.com/v1/oauth/token")
oauth.put_ClientId("APP_KEY")
oauth.put_ClientSecret("APP_SECRET")
oauth.put_UseBasicAuth(true)
oauth.put_CodeChallenge(false)
# The registered HTTPS callback forwards to this desktop's HTTP listener.
oauth.put_AppCallbackUrl("https://127.0.0.1:8182/")
oauth.put_LocalHost("127.0.0.1")
oauth.put_ListenPort(3017)
url = oauth.startAuth()
if (oauth.get_LastMethodSuccess() != true)
print "Could not start authorization. Check the callback settings and free the listener port." + "\n";
exit
end
success = oauth.LaunchBrowser(url)
if (success != true)
oauth.Cancel()
print "Could not open the system browser." + "\n";
exit
end
# Allow time for sign-in, MFA, and account selection.
waitedMs = 0
while (waitedMs < 300000) and (oauth.get_AuthFlowState() < 3)
oauth.SleepMs(100)
waitedMs = waitedMs + 100
end
state = oauth.get_AuthFlowState()
if (state < 3)
oauth.Cancel()
print "Authorization timed out. Check the HTTPS callback and try again." + "\n";
exit
end
if (state == 4)
print "Authorization was declined." + "\n";
exit
end
if (state != 3)
print "Authorization failed. Check app approval, credentials, and the exact callback URL." + "\n";
exit
end
reply = Chilkat::CkJsonObject.new()
success = reply.Load(oauth.accessTokenResponse())
if (success != true)
print "Unexpected token response." + "\n";
exit
end
if (reply.HasMember("access_token") != true)
print "The token response contained no access token." + "\n";
exit
end
access = reply.stringOf("access_token")
lifetime = reply.IntOf("expires_in")
if ((access == "") or (lifetime <= 0))
print "The token response lacks a usable access token or lifetime." + "\n";
exit
end
if (reply.HasMember("refresh_token") != true)
print "No refresh token in the authorization response." + "\n";
exit
end
refresh = reply.stringOf("refresh_token")
if (refresh == "")
print "The refresh token is empty." + "\n";
exit
end
vault = Chilkat::CkSecrets.new()
vault.put_Location("local_manager")
vault.put_AutoSplit(true)
entry = Chilkat::CkJsonObject.new()
entry.UpdateString("appName","SchwabTrader")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","personal-desktop")
tokens = Chilkat::CkJsonObject.new()
tokens.UpdateString("refresh_token",refresh)
now = Chilkat::CkDateTime.new()
now.SetFromCurrentSystemTime()
tokens.UpdateString("authorized_at_utc",now.getAsTimestamp(false))
tokens.UpdateString("obtained_at_utc",now.getAsTimestamp(false))
tokens.UpdateString("access_token",access)
tokens.UpdateInt("expires_in",lifetime)
success = vault.UpdateSecretJson(entry,tokens)
if (success != true)
print "Could not save the new credentials. Check the configured secret store." + "\n";
exit
end
print "Saved credentials. Access token lifetime in seconds: " + lifetime.to_s() + "\n";