Xojo Plugin Requires Chilkat v11.0.0+
Xojo Plugin
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.
Chilkat Xojo Plugin Downloads
Dim success As Boolean
success = False
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim vault As New Chilkat.Secrets
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As New Chilkat.JsonObject
success = entry.UpdateString("appName","SchwabTrader")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","personal-desktop")
Dim tokens As New Chilkat.JsonObject
success = vault.GetSecretJson(entry,tokens)
If (success <> True) Then
System.DebugLog("Could not read the token record. Check the store and complete desktop authorization first.")
Return
End If
Dim accessToken As String
accessToken = tokens.StringOf("access_token")
If (accessToken = "") Then
System.DebugLog("Run the Schwab authorization or refresh example to save an access token.")
Return
End If
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.AuthToken = accessToken
// Supply one of these from your account picker. Leave both empty to list only.
Dim selectedEnding As String
selectedEnding = ""
Dim selectedFullNumber As String
selectedFullNumber = ""
Dim resp As New Chilkat.HttpResponse
success = http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp)
If ((success <> True) Or (resp.StatusCode <> 200)) Then
If (resp.StatusCode = 0) Then
System.DebugLog("No HTTP response. Check connectivity.")
Else
System.DebugLog("Schwab returned HTTP " + Str(resp.StatusCode))
If (resp.StatusCode = 401) Then
System.DebugLog("Refresh the access token or repeat desktop authorization.")
End If
End If
Return
End If
Dim accounts As New Chilkat.JsonArray
success = accounts.Load(resp.BodyStr)
If (success <> True) Then
System.DebugLog("Schwab returned unexpected JSON.")
Return
End If
Dim row As New Chilkat.JsonObject
Dim number As New Chilkat.StringBuilder
Dim matches As Int32
matches = 0
Dim i As Int32
i = 0
Dim selectedHash As String
selectedHash = ""
While (i < accounts.Size)
success = accounts.ObjectAt2(i,row)
Dim accountNumber As String
accountNumber = row.StringOf("accountNumber")
success = number.SetString(accountNumber)
If (number.Length >= 4) Then
Dim start As Int32
start = number.Length - 4
Dim ending As String
ending = number.GetRange(start,4,False)
System.DebugLog("Account ending in " + ending)
Dim matched As Boolean
matched = False
If (selectedFullNumber <> "") Then
If (accountNumber = selectedFullNumber) Then
matched = True
End If
Else
If (ending = selectedEnding) Then
matched = True
End If
End If
If (matched = True) Then
matches = matches + 1
selectedHash = row.StringOf("hashValue")
End If
End If
i = i + 1
Wend
If ((matches <> 1) Or (selectedHash = "")) Then
System.DebugLog("Select exactly one account. For duplicate endings use the full-account input.")
Return
End If
success = entry.UpdateString("service","SelectedAccount")
Dim selection As New Chilkat.JsonObject
success = selection.UpdateString("hashValue",selectedHash)
success = vault.UpdateSecretJson(entry,selection)
If (success <> True) Then
System.DebugLog("Could not save the selected account.")
Return
End If
System.DebugLog("Selected account saved.")