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

Schwab: List authorized accounts and select one

See more Schwab Examples

Lists masked account numbers. Run once to see their endings, then enter the last four digits of the intended account. A unique match saves Schwab’s hashValue in a separate secret for the account-specific examples. If two accounts share an ending, use the full-account input from your account picker. A failed match leaves the previous selection in place.

Use the access token saved by desktop authorization. Use the same Chilkat Secrets store as the OAuth example.

Schwab API specification

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

accessToken = tokens.StringOf("access_token")
If (accessToken = "") Then
    Response.Write "<pre>" & Server.HTMLEncode( "Run the Schwab authorization or refresh example to save an access token.") & "</pre>"
    Response.End
End If

set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = accessToken

'  Supply one of these from your account picker. Leave both empty to list only.
selectedEnding = ""
selectedFullNumber = ""
set resp = Server.CreateObject("Chilkat.HttpResponse")
success = http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp)
If ((success <> 1) Or (resp.StatusCode <> 200)) Then
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response. Check connectivity.") & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "Schwab returned HTTP " & resp.StatusCode) & "</pre>"
        If (resp.StatusCode = 401) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Refresh the access token or repeat desktop authorization.") & "</pre>"
        End If

    End If

    Response.End
End If

set accounts = Server.CreateObject("Chilkat.JsonArray")
success = accounts.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Schwab returned unexpected JSON.") & "</pre>"
    Response.End
End If

set row = Server.CreateObject("Chilkat.JsonObject")
set number = Server.CreateObject("Chilkat.StringBuilder")
matches = 0
i = 0
selectedHash = ""
Do While (i < accounts.Size)
    success = accounts.ObjectAt2(i,row)
    accountNumber = row.StringOf("accountNumber")
    success = number.SetString(accountNumber)
    If (number.Length >= 4) Then
        start = number.Length - 4
        ending = number.GetRange(start,4,0)
        Response.Write "<pre>" & Server.HTMLEncode( "Account ending in " & ending) & "</pre>"
        matched = 0
        If (selectedFullNumber <> "") Then
            If (accountNumber = selectedFullNumber) Then
                matched = 1
            End If

        Else
            If (ending = selectedEnding) Then
                matched = 1
            End If

        End If

        If (matched = 1) Then
            matches = matches + 1
            selectedHash = row.StringOf("hashValue")
        End If

    End If

    i = i + 1
Loop
If ((matches <> 1) Or (selectedHash = "")) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Select exactly one account. For duplicate endings use the full-account input.") & "</pre>"
    Response.End
End If

success = entry.UpdateString("service","SelectedAccount")
set selection = Server.CreateObject("Chilkat.JsonObject")
success = selection.UpdateString("hashValue",selectedHash)
success = vault.UpdateSecretJson(entry,selection)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not save the selected account.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Selected account saved.") & "</pre>"

%>
</body>
</html>