Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Schwab OAuth 2.0: Refresh an access token

See more Schwab Examples

Run Authorize a desktop application first. Use the same app credentials and saved grant. Schwab documents 30 minutes for the access token and seven days from creation for the refresh token. Schedule refreshes using the returned expires_in and the saved UTC acquisition time. After the refresh window expires, repeat browser sign-in and consent.

The example sends a form-encoded refresh grant with HTTP Basic client authentication. It saves the returned access token and lifetime, adopts a replacement refresh token when supplied, and retains the original grant time. Use the saved access token in the Authorization: Bearer header on Schwab API requests. Coordinate refreshes if multiple processes share the same record.

If you used the earlier file-based example, run desktop authorization once to populate the secret store. After updating your application to use that store, retire the old schwab-access-token.json file.

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_Vault
oleobject loo_Entry
oleobject loo_Tokens
string ls_Refresh
oleobject loo_Http
oleobject loo_Form
oleobject loo_Resp
oleobject loo_Problem
oleobject loo_Reply
string ls_Access
long li_Lifetime
string ls_Replacement
oleobject loo_Now

li_Success = 0

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

loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
    destroy loo_Vault
    MessageBox("Error","Connecting to COM object failed")
    return
end if
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")

li_Success = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Success <> 1 then
    Write-Debug "Could not read the token record. Check the store and complete desktop authorization first."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

if loo_Tokens.HasMember("refresh_token") <> 1 then
    Write-Debug "No saved refresh token. Complete desktop authorization."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

ls_Refresh = loo_Tokens.StringOf("refresh_token")
if ls_Refresh = "" then
    Write-Debug "The saved refresh token is empty. Complete desktop authorization."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
loo_Http.Login = "APP_KEY"
loo_Http.Password = "APP_SECRET"
loo_Form = create oleobject
li_rc = loo_Form.ConnectToNewObject("Chilkat.HttpRequest")

loo_Form.HttpVerb = "POST"
loo_Form.ContentType = "application/x-www-form-urlencoded"
loo_Form.AddParam("grant_type","refresh_token")
loo_Form.AddParam("refresh_token",ls_Refresh)
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpReq("https://api.schwabapi.com/v1/oauth/token",loo_Form,loo_Resp)
if li_Success <> 1 then
    if loo_Resp.StatusCode = 0 then
        Write-Debug "No HTTP response. Check connectivity before retrying."
        destroy loo_Vault
        destroy loo_Entry
        destroy loo_Tokens
        destroy loo_Http
        destroy loo_Form
        destroy loo_Resp
        return
    end if

end if

if loo_Resp.StatusCode <> 200 then
    loo_Problem = create oleobject
    li_rc = loo_Problem.ConnectToNewObject("Chilkat.JsonObject")

    li_Success = loo_Problem.Load(loo_Resp.BodyStr)
    Write-Debug "Schwab rejected the refresh. HTTP " + string(loo_Resp.StatusCode)
    if li_Success = 1 then
        if loo_Problem.StringOfEquals("error","invalid_grant",1) = 1 then
            Write-Debug "The grant is expired or invalid. Run desktop authorization again."
        end if

        if loo_Problem.StringOfEquals("error","invalid_client",1) = 1 then
            Write-Debug "Check the app key and secret. If the seven-day grant has expired, authorize again."
        end if

    end if

    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Form
    destroy loo_Resp
    destroy loo_Problem
    return
end if

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

li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Unexpected token response."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Form
    destroy loo_Resp
    destroy loo_Problem
    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_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Form
    destroy loo_Resp
    destroy loo_Problem
    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_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Form
    destroy loo_Resp
    destroy loo_Problem
    destroy loo_Reply
    return
end if

//  A refresh response can omit the refresh token. Update it only when a new value is supplied.
if loo_Reply.HasMember("refresh_token") = 1 then
    ls_Replacement = loo_Reply.StringOf("refresh_token")
    if ls_Replacement <> "" then
        loo_Tokens.UpdateString("refresh_token",ls_Replacement)
    end if

end if

loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")

loo_Now.SetFromCurrentSystemTime()
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_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Form
    destroy loo_Resp
    destroy loo_Problem
    destroy loo_Reply
    destroy loo_Now
    return
end if

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


destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Problem
destroy loo_Reply
destroy loo_Now