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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lcRefresh
LOCAL loHttp
LOCAL loForm
LOCAL loResp
LOCAL loProblem
LOCAL loReply
LOCAL lcAccess
LOCAL lnLifetime
LOCAL lcReplacement
LOCAL loNow
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
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')
lnSuccess = loVault.GetSecretJson(loEntry,loTokens)
IF (lnSuccess <> 1) THEN
? "Could not read the token record. Check the store and complete desktop authorization first."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
CANCEL
ENDIF
IF (loTokens.HasMember("refresh_token") <> 1) THEN
? "No saved refresh token. Complete desktop authorization."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
CANCEL
ENDIF
lcRefresh = loTokens.StringOf("refresh_token")
IF (lcRefresh = "") THEN
? "The saved refresh token is empty. Complete desktop authorization."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.BasicAuth = 1
loHttp.Login = "APP_KEY"
loHttp.Password = "APP_SECRET"
loForm = CreateObject('Chilkat.HttpRequest')
loForm.HttpVerb = "POST"
loForm.ContentType = "application/x-www-form-urlencoded"
loForm.AddParam("grant_type","refresh_token")
loForm.AddParam("refresh_token",lcRefresh)
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpReq("https://api.schwabapi.com/v1/oauth/token",loForm,loResp)
IF (lnSuccess <> 1) THEN
IF (loResp.StatusCode = 0) THEN
? "No HTTP response. Check connectivity before retrying."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loForm
RELEASE loResp
CANCEL
ENDIF
ENDIF
IF (loResp.StatusCode <> 200) THEN
loProblem = CreateObject('Chilkat.JsonObject')
lnSuccess = loProblem.Load(loResp.BodyStr)
? "Schwab rejected the refresh. HTTP " + STR(loResp.StatusCode)
IF (lnSuccess = 1) THEN
IF (loProblem.StringOfEquals("error","invalid_grant",1) = 1) THEN
? "The grant is expired or invalid. Run desktop authorization again."
ENDIF
IF (loProblem.StringOfEquals("error","invalid_client",1) = 1) THEN
? "Check the app key and secret. If the seven-day grant has expired, authorize again."
ENDIF
ENDIF
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loForm
RELEASE loResp
RELEASE loProblem
CANCEL
ENDIF
loReply = CreateObject('Chilkat.JsonObject')
lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Unexpected token response."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loForm
RELEASE loResp
RELEASE loProblem
RELEASE loReply
CANCEL
ENDIF
IF (loReply.HasMember("access_token") <> 1) THEN
? "The token response contained no access token."
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loForm
RELEASE loResp
RELEASE loProblem
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 loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loForm
RELEASE loResp
RELEASE loProblem
RELEASE loReply
CANCEL
ENDIF
* A refresh response can omit the refresh token. Update it only when a new value is supplied.
IF (loReply.HasMember("refresh_token") = 1) THEN
lcReplacement = loReply.StringOf("refresh_token")
IF (lcReplacement <> "") THEN
loTokens.UpdateString("refresh_token",lcReplacement)
ENDIF
ENDIF
loNow = CreateObject('Chilkat.CkDateTime')
loNow.SetFromCurrentSystemTime()
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 loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loForm
RELEASE loResp
RELEASE loProblem
RELEASE loReply
RELEASE loNow
CANCEL
ENDIF
? "Saved credentials. Access token lifetime in seconds: " + STR(lnLifetime)
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loForm
RELEASE loResp
RELEASE loProblem
RELEASE loReply
RELEASE loNow