Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL cAccessToken
LOCAL oHttp
LOCAL cSelectedEnding
LOCAL cSelectedFullNumber
LOCAL oResp
LOCAL oAccounts
LOCAL oRow
LOCAL oNumber
LOCAL nMatches
LOCAL i
LOCAL cSelectedHash
LOCAL cAccountNumber
LOCAL nStart
LOCAL cEnding
LOCAL nMatched
LOCAL oSelection
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0
oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "SchwabTrader")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "personal-desktop")
oTokens := CreateObject("Chilkat.JsonObject")
nSuccess := oVault:GetSecretJson(oEntry, oTokens)
IF (nSuccess != 1)
? "Could not read the token record. Check the store and complete desktop authorization first."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
RETURN
ENDIF
cAccessToken := oTokens:StringOf("access_token")
IF (cAccessToken == "")
? "Run the Schwab authorization or refresh example to save an access token."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
RETURN
ENDIF
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := cAccessToken
// Supply one of these from your account picker. Leave both empty to list only.
cSelectedEnding := ""
cSelectedFullNumber := ""
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("GET", "https://api.schwabapi.com/trader/v1/accounts/accountNumbers", oResp)
IF ((nSuccess != 1) .OR. (oResp:StatusCode != 200))
IF (oResp:StatusCode == 0)
? "No HTTP response. Check connectivity."
ELSE
? "Schwab returned HTTP " + Str(oResp:StatusCode)
IF (oResp:StatusCode == 401)
? "Refresh the access token or repeat desktop authorization."
ENDIF
ENDIF
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
oAccounts := CreateObject("Chilkat.JsonArray")
nSuccess := oAccounts:Load(oResp:BodyStr)
IF (nSuccess != 1)
? "Schwab returned unexpected JSON."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oAccounts:destroy()
RETURN
ENDIF
oRow := CreateObject("Chilkat.JsonObject")
oNumber := CreateObject("Chilkat.StringBuilder")
nMatches := 0
i := 0
cSelectedHash := ""
DO WHILE (i < oAccounts:Size)
oAccounts:ObjectAt2(i, oRow)
cAccountNumber := oRow:StringOf("accountNumber")
oNumber:SetString(cAccountNumber)
IF (oNumber:Length >= 4)
nStart := oNumber:Length - 4
cEnding := oNumber:GetRange(nStart, 4, 0)
? "Account ending in " + cEnding
nMatched := 0
IF (cSelectedFullNumber != "")
IF (cAccountNumber == cSelectedFullNumber)
nMatched := 1
ENDIF
ELSE
IF (cEnding == cSelectedEnding)
nMatched := 1
ENDIF
ENDIF
IF (nMatched == 1)
nMatches := nMatches + 1
cSelectedHash := oRow:StringOf("hashValue")
ENDIF
ENDIF
i := i + 1
ENDDO
IF ((nMatches != 1) .OR. (cSelectedHash == ""))
? "Select exactly one account. For duplicate endings use the full-account input."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oAccounts:destroy()
oRow:destroy()
oNumber:destroy()
RETURN
ENDIF
oEntry:UpdateString("service", "SelectedAccount")
oSelection := CreateObject("Chilkat.JsonObject")
oSelection:UpdateString("hashValue", cSelectedHash)
nSuccess := oVault:UpdateSecretJson(oEntry, oSelection)
IF (nSuccess != 1)
? "Could not save the selected account."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oAccounts:destroy()
oRow:destroy()
oNumber:destroy()
oSelection:destroy()
RETURN
ENDIF
? "Selected account saved."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oAccounts:destroy()
oRow:destroy()
oNumber:destroy()
oSelection:destroy()