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

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;

    CkSecrets vault = new CkSecrets();
    vault.put_Location("local_manager");
    vault.put_AutoSplit(true);
    CkJsonObject entry = new CkJsonObject();
    entry.UpdateString("appName","SchwabTrader");
    entry.UpdateString("service","OAuth2");
    entry.UpdateString("username","personal-desktop");
    CkJsonObject tokens = new CkJsonObject();
    success = vault.GetSecretJson(entry,tokens);
    if (success != true) {
        System.out.println("Could not read the token record. Check the store and complete desktop authorization first.");
        return;
        }

    String accessToken = tokens.stringOf("access_token");
    if (accessToken == "") {
        System.out.println("Run the Schwab authorization or refresh example to save an access token.");
        return;
        }

    CkHttp http = new CkHttp();
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(accessToken);

    //  Supply one of these from your account picker. Leave both empty to list only.
    String selectedEnding = "";
    String selectedFullNumber = "";
    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp);
    if ((success != true) || (resp.get_StatusCode() != 200)) {
        if (resp.get_StatusCode() == 0) {
            System.out.println("No HTTP response. Check connectivity.");
            }
        else {
            System.out.println("Schwab returned HTTP " + resp.get_StatusCode());
            if (resp.get_StatusCode() == 401) {
                System.out.println("Refresh the access token or repeat desktop authorization.");
                }

            }

        return;
        }

    CkJsonArray accounts = new CkJsonArray();
    success = accounts.Load(resp.bodyStr());
    if (success != true) {
        System.out.println("Schwab returned unexpected JSON.");
        return;
        }

    CkJsonObject row = new CkJsonObject();
    CkStringBuilder number = new CkStringBuilder();
    int matches = 0;
    int i = 0;
    String selectedHash = "";
    while ((i < accounts.get_Size())) {
        accounts.ObjectAt2(i,row);
        String accountNumber = row.stringOf("accountNumber");
        number.SetString(accountNumber);
        if (number.get_Length() >= 4) {
            int start = number.get_Length() - 4;
            String ending = number.getRange(start,4,false);
            System.out.println("Account ending in " + ending);
            boolean matched = false;
            if (selectedFullNumber != "") {
                if (accountNumber == selectedFullNumber) {
                    matched = true;
                    }

                }
            else {
                if (ending == selectedEnding) {
                    matched = true;
                    }

                }

            if (matched == true) {
                matches = matches+1;
                selectedHash = row.stringOf("hashValue");
                }

            }

        i = i+1;
        }

    if ((matches != 1) || (selectedHash == "")) {
        System.out.println("Select exactly one account. For duplicate endings use the full-account input.");
        return;
        }

    entry.UpdateString("service","SelectedAccount");
    CkJsonObject selection = new CkJsonObject();
    selection.UpdateString("hashValue",selectedHash);
    success = vault.UpdateSecretJson(entry,selection);
    if (success != true) {
        System.out.println("Could not save the selected account.");
        return;
        }

    System.out.println("Selected account saved.");
  }
}