DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoVault
Variant vEntry
Handle hoEntry
Variant vTokens
Handle hoTokens
String sAccessToken
Handle hoHttp
String sSelectedEnding
String sSelectedFullNumber
Variant vResp
Handle hoResp
Handle hoAccounts
Variant vRow
Handle hoRow
Handle hoNumber
Integer iMatches
Integer i
String sSelectedHash
String sAccountNumber
Integer iStart
String sEnding
Boolean iMatched
Variant vSelection
Handle hoSelection
String sTemp1
Integer iTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
Get Create (RefClass(cComChilkatSecrets)) To hoVault
If (Not(IsComObjectCreated(hoVault))) Begin
Send CreateComObject of hoVault
End
Set ComLocation Of hoVault To "local_manager"
Set ComAutoSplit Of hoVault To True
Get Create (RefClass(cComChilkatJsonObject)) To hoEntry
If (Not(IsComObjectCreated(hoEntry))) Begin
Send CreateComObject of hoEntry
End
Get ComUpdateString Of hoEntry "appName" "SchwabTrader" To iSuccess
Get ComUpdateString Of hoEntry "service" "OAuth2" To iSuccess
Get ComUpdateString Of hoEntry "username" "personal-desktop" To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
If (Not(IsComObjectCreated(hoTokens))) Begin
Send CreateComObject of hoTokens
End
Get pvComObject of hoEntry to vEntry
Get pvComObject of hoTokens to vTokens
Get ComGetSecretJson Of hoVault vEntry vTokens To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read the token record. Check the store and complete desktop authorization first."
Procedure_Return
End
Get ComStringOf Of hoTokens "access_token" To sAccessToken
If (sAccessToken = "") Begin
Showln "Run the Schwab authorization or refresh example to save an access token."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComAuthToken Of hoHttp To sAccessToken
// Supply one of these from your account picker. Leave both empty to list only.
Move "" To sSelectedEnding
Move "" To sSelectedFullNumber
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" "https://api.schwabapi.com/trader/v1/accounts/accountNumbers" vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "No HTTP response. Check connectivity."
End
Else Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Schwab returned HTTP " iTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 401) Begin
Showln "Refresh the access token or repeat desktop authorization."
End
End
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonArray)) To hoAccounts
If (Not(IsComObjectCreated(hoAccounts))) Begin
Send CreateComObject of hoAccounts
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoAccounts sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Schwab returned unexpected JSON."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoRow
If (Not(IsComObjectCreated(hoRow))) Begin
Send CreateComObject of hoRow
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoNumber
If (Not(IsComObjectCreated(hoNumber))) Begin
Send CreateComObject of hoNumber
End
Move 0 To iMatches
Move 0 To i
Move "" To sSelectedHash
While (i < (ComSize(hoAccounts)))
Get pvComObject of hoRow to vRow
Get ComObjectAt2 Of hoAccounts i vRow To iSuccess
Get ComStringOf Of hoRow "accountNumber" To sAccountNumber
Get ComSetString Of hoNumber sAccountNumber To iSuccess
Get ComLength Of hoNumber To iTemp1
If (iTemp1 >= 4) Begin
Get ComLength Of hoNumber To iTemp1
Move (iTemp1 - 4) To iStart
Get ComGetRange Of hoNumber iStart 4 False To sEnding
Showln "Account ending in " sEnding
Move False To iMatched
If (sSelectedFullNumber <> "") Begin
If (sAccountNumber = sSelectedFullNumber) Begin
Move True To iMatched
End
End
Else Begin
If (sEnding = sSelectedEnding) Begin
Move True To iMatched
End
End
If (iMatched = True) Begin
Move (iMatches + 1) To iMatches
Get ComStringOf Of hoRow "hashValue" To sSelectedHash
End
End
Move (i + 1) To i
Loop
If ((iMatches <> 1) Or (sSelectedHash = "")) Begin
Showln "Select exactly one account. For duplicate endings use the full-account input."
Procedure_Return
End
Get ComUpdateString Of hoEntry "service" "SelectedAccount" To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoSelection
If (Not(IsComObjectCreated(hoSelection))) Begin
Send CreateComObject of hoSelection
End
Get ComUpdateString Of hoSelection "hashValue" sSelectedHash To iSuccess
Get pvComObject of hoEntry to vEntry
Get pvComObject of hoSelection to vSelection
Get ComUpdateSecretJson Of hoVault vEntry vSelection To iSuccess
If (iSuccess <> True) Begin
Showln "Could not save the selected account."
Procedure_Return
End
Showln "Selected account saved."
End_Procedure