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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0

set vault = Server.CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = Server.CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","SchwabTrader")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","personal-desktop")
set tokens = Server.CreateObject("Chilkat.JsonObject")
success = vault.GetSecretJson(entry,tokens)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read the token record. Check the store and complete desktop authorization first.") & "</pre>"
    Response.End
End If

If (tokens.HasMember("refresh_token") <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "No saved refresh token. Complete desktop authorization.") & "</pre>"
    Response.End
End If

refresh = tokens.StringOf("refresh_token")
If (refresh = "") Then
    Response.Write "<pre>" & Server.HTMLEncode( "The saved refresh token is empty. Complete desktop authorization.") & "</pre>"
    Response.End
End If

set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.BasicAuth = 1
http.Login = "APP_KEY"
http.Password = "APP_SECRET"
set form = Server.CreateObject("Chilkat.HttpRequest")
form.HttpVerb = "POST"
form.ContentType = "application/x-www-form-urlencoded"
form.AddParam "grant_type","refresh_token"
form.AddParam "refresh_token",refresh
set resp = Server.CreateObject("Chilkat.HttpResponse")
success = http.HttpReq("https://api.schwabapi.com/v1/oauth/token",form,resp)
If (success <> 1) Then
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response. Check connectivity before retrying.") & "</pre>"
        Response.End
    End If

End If

If (resp.StatusCode <> 200) Then
    set problem = Server.CreateObject("Chilkat.JsonObject")
    success = problem.Load(resp.BodyStr)
    Response.Write "<pre>" & Server.HTMLEncode( "Schwab rejected the refresh. HTTP " & resp.StatusCode) & "</pre>"
    If (success = 1) Then
        If (problem.StringOfEquals("error","invalid_grant",1) = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "The grant is expired or invalid. Run desktop authorization again.") & "</pre>"
        End If

        If (problem.StringOfEquals("error","invalid_client",1) = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Check the app key and secret. If the seven-day grant has expired, authorize again.") & "</pre>"
        End If

    End If

    Response.End
End If

set reply = Server.CreateObject("Chilkat.JsonObject")
success = reply.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected token response.") & "</pre>"
    Response.End
End If

If (reply.HasMember("access_token") <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "The token response contained no access token.") & "</pre>"
    Response.End
End If

access = reply.StringOf("access_token")
lifetime = reply.IntOf("expires_in")
If ((access = "") Or (lifetime <= 0)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "The token response lacks a usable access token or lifetime.") & "</pre>"
    Response.End
End If

'  A refresh response can omit the refresh token. Update it only when a new value is supplied.
If (reply.HasMember("refresh_token") = 1) Then
    replacement = reply.StringOf("refresh_token")
    If (replacement <> "") Then
        success = tokens.UpdateString("refresh_token",replacement)
    End If

End If

set now = Server.CreateObject("Chilkat.CkDateTime")
success = now.SetFromCurrentSystemTime()
success = tokens.UpdateString("obtained_at_utc",now.GetAsTimestamp(0))
success = tokens.UpdateString("access_token",access)
success = tokens.UpdateInt("expires_in",lifetime)
success = vault.UpdateSecretJson(entry,tokens)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not save the new credentials. Check the configured secret store.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Saved credentials. Access token lifetime in seconds: " & lifetime) & "</pre>"

%>
</body>
</html>