AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
$oVault = ObjCreate("Chilkat.Secrets")
$oVault.Location = "local_manager"
$oVault.AutoSplit = True
$oEntry = ObjCreate("Chilkat.JsonObject")
$oEntry.UpdateString("appName","SchwabTrader")
$oEntry.UpdateString("service","OAuth2")
$oEntry.UpdateString("username","personal-desktop")
$oTokens = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oVault.GetSecretJson($oEntry,$oTokens)
If ($bSuccess <> True) Then
ConsoleWrite("Could not read the token record. Check the store and complete desktop authorization first." & @CRLF)
Exit
EndIf
Local $sAccessToken = $oTokens.StringOf("access_token")
If ($sAccessToken = "") Then
ConsoleWrite("Run the Schwab authorization or refresh example to save an access token." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $sAccessToken
; Supply one of these from your account picker. Leave both empty to list only.
Local $selectedEnding = ""
Local $selectedFullNumber = ""
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",$oResp)
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
If ($oResp.StatusCode = 0) Then
ConsoleWrite("No HTTP response. Check connectivity." & @CRLF)
Else
ConsoleWrite("Schwab returned HTTP " & $oResp.StatusCode & @CRLF)
If ($oResp.StatusCode = 401) Then
ConsoleWrite("Refresh the access token or repeat desktop authorization." & @CRLF)
EndIf
EndIf
Exit
EndIf
$oAccounts = ObjCreate("Chilkat.JsonArray")
$bSuccess = $oAccounts.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Schwab returned unexpected JSON." & @CRLF)
Exit
EndIf
$oRow = ObjCreate("Chilkat.JsonObject")
$oNumber = ObjCreate("Chilkat.StringBuilder")
Local $iMatches = 0
Local $i = 0
Local $selectedHash = ""
While ($i < $oAccounts.Size)
$oAccounts.ObjectAt2($i,$oRow)
Local $sAccountNumber = $oRow.StringOf("accountNumber")
$oNumber.SetString($sAccountNumber)
If ($oNumber.Length >= 4) Then
Local $iStart = $oNumber.Length - 4
Local $sEnding = $oNumber.GetRange($iStart,4,False)
ConsoleWrite("Account ending in " & $sEnding & @CRLF)
Local $bMatched = False
If ($selectedFullNumber <> "") Then
If ($sAccountNumber = $selectedFullNumber) Then
$bMatched = True
EndIf
Else
If ($sEnding = $selectedEnding) Then
$bMatched = True
EndIf
EndIf
If ($bMatched = True) Then
$iMatches = $iMatches + 1
$selectedHash = $oRow.StringOf("hashValue")
EndIf
EndIf
$i = $i + 1
Wend
If (($iMatches <> 1) Or ($selectedHash = "")) Then
ConsoleWrite("Select exactly one account. For duplicate endings use the full-account input." & @CRLF)
Exit
EndIf
$oEntry.UpdateString("service","SelectedAccount")
$oSelection = ObjCreate("Chilkat.JsonObject")
$oSelection.UpdateString("hashValue",$selectedHash)
$bSuccess = $oVault.UpdateSecretJson($oEntry,$oSelection)
If ($bSuccess <> True) Then
ConsoleWrite("Could not save the selected account." & @CRLF)
Exit
EndIf
ConsoleWrite("Selected account saved." & @CRLF)