Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
string ls_AccessToken
oleobject loo_Http
string ls_SelectedEnding
string ls_SelectedFullNumber
oleobject loo_Resp
oleobject loo_Accounts
oleobject loo_Row
oleobject loo_Number
long li_Matches
long i
string ls_SelectedHash
string ls_AccountNumber
long li_Start
string ls_Ending
long li_Matched
oleobject loo_Selection

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0

loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
    destroy loo_Vault
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")

loo_Entry.UpdateString("appName","SchwabTrader")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","personal-desktop")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Success <> 1 then
    Write-Debug "Could not read the token record. Check the store and complete desktop authorization first."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

ls_AccessToken = loo_Tokens.StringOf("access_token")
if ls_AccessToken = "" then
    Write-Debug "Run the Schwab authorization or refresh example to save an access token."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = ls_AccessToken

//  Supply one of these from your account picker. Leave both empty to list only.
ls_SelectedEnding = ""
ls_SelectedFullNumber = ""
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",loo_Resp)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
    if loo_Resp.StatusCode = 0 then
        Write-Debug "No HTTP response. Check connectivity."
    else
        Write-Debug "Schwab returned HTTP " + string(loo_Resp.StatusCode)
        if loo_Resp.StatusCode = 401 then
            Write-Debug "Refresh the access token or repeat desktop authorization."
        end if

    end if

    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    return
end if

loo_Accounts = create oleobject
li_rc = loo_Accounts.ConnectToNewObject("Chilkat.JsonArray")

li_Success = loo_Accounts.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Schwab returned unexpected JSON."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Accounts
    return
end if

loo_Row = create oleobject
li_rc = loo_Row.ConnectToNewObject("Chilkat.JsonObject")

loo_Number = create oleobject
li_rc = loo_Number.ConnectToNewObject("Chilkat.StringBuilder")

li_Matches = 0
i = 0
ls_SelectedHash = ""
do while (i < loo_Accounts.Size)
    loo_Accounts.ObjectAt2(i,loo_Row)
    ls_AccountNumber = loo_Row.StringOf("accountNumber")
    loo_Number.SetString(ls_AccountNumber)
    if loo_Number.Length >= 4 then
        li_Start = loo_Number.Length - 4
        ls_Ending = loo_Number.GetRange(li_Start,4,0)
        Write-Debug "Account ending in " + ls_Ending
        li_Matched = 0
        if ls_SelectedFullNumber <> "" then
            if ls_AccountNumber = ls_SelectedFullNumber then
                li_Matched = 1
            end if

        else
            if ls_Ending = ls_SelectedEnding then
                li_Matched = 1
            end if

        end if

        if li_Matched = 1 then
            li_Matches = li_Matches + 1
            ls_SelectedHash = loo_Row.StringOf("hashValue")
        end if

    end if

    i = i + 1
loop
if (li_Matches <> 1) OR (ls_SelectedHash = "") then
    Write-Debug "Select exactly one account. For duplicate endings use the full-account input."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Accounts
    destroy loo_Row
    destroy loo_Number
    return
end if

loo_Entry.UpdateString("service","SelectedAccount")
loo_Selection = create oleobject
li_rc = loo_Selection.ConnectToNewObject("Chilkat.JsonObject")

loo_Selection.UpdateString("hashValue",ls_SelectedHash)
li_Success = loo_Vault.UpdateSecretJson(loo_Entry,loo_Selection)
if li_Success <> 1 then
    Write-Debug "Could not save the selected account."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Accounts
    destroy loo_Row
    destroy loo_Number
    destroy loo_Selection
    return
end if

Write-Debug "Selected account saved."


destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Accounts
destroy loo_Row
destroy loo_Number
destroy loo_Selection