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

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false

local oauth = chilkat.OAuth2.new()
oauth.authorization_endpoint = "https://api.schwabapi.com/v1/oauth/authorize"
oauth.token_endpoint = "https://api.schwabapi.com/v1/oauth/token"
oauth.client_id = "APP_KEY"
oauth.client_secret = "APP_SECRET"
oauth.use_basic_auth = true
oauth.code_challenge = false

-- The registered HTTPS callback forwards to this desktop's HTTP listener.
oauth.app_callback_url = "https://127.0.0.1:8182/"
oauth.local_host = "127.0.0.1"
oauth.listen_port = 3017
local url = oauth:start_auth()
if not oauth.last_method_success then
    print("Could not start authorization. Check the callback settings and free the listener port.")
    return
end

success = oauth:launch_browser(url)
if not success then
    oauth:cancel()
    print("Could not open the system browser.")
    return
end

-- Allow time for sign-in, MFA, and account selection.
local waitedMs = 0
while (waitedMs < 300000) and (oauth.auth_flow_state < 3) do
    oauth:sleep_ms(100)
    waitedMs = waitedMs + 100
end

local state = oauth.auth_flow_state
if state < 3 then
    oauth:cancel()
    print("Authorization timed out. Check the HTTPS callback and try again.")
    return
end

if state == 4 then
    print("Authorization was declined.")
    return
end

if state ~= 3 then
    print("Authorization failed. Check app approval, credentials, and the exact callback URL.")
    return
end

local reply = chilkat.JsonObject.new()
success = reply:load(oauth.access_token_response)
if not success then
    print("Unexpected token response.")
    return
end

if not reply:has_member("access_token") then
    print("The token response contained no access token.")
    return
end

local access = reply:string_of("access_token")
local lifetime = reply:int_of("expires_in")
if (access == "") or (lifetime <= 0) then
    print("The token response lacks a usable access token or lifetime.")
    return
end

if not reply:has_member("refresh_token") then
    print("No refresh token in the authorization response.")
    return
end

local refresh = reply:string_of("refresh_token")
if refresh == "" then
    print("The refresh token is empty.")
    return
end

local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "SchwabTrader")
entry:update_string("service", "OAuth2")
entry:update_string("username", "personal-desktop")
local tokens = chilkat.JsonObject.new()
tokens:update_string("refresh_token", refresh)
local now = chilkat.DateTime.new()
now:set_from_current_system_time()
tokens:update_string("authorized_at_utc", now:get_as_timestamp(false))
tokens:update_string("obtained_at_utc", now:get_as_timestamp(false))
tokens:update_string("access_token", access)
tokens:update_int("expires_in", lifetime)
success = vault:update_secret_json(entry, tokens)
if not success then
    print("Could not save the new credentials. Check the configured secret store.")
    return
end

print("Saved credentials. Access token lifetime in seconds: " .. lifetime)