Java Requires Chilkat v11.0.0+
Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonArray;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Secrets;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Secrets vault = new Secrets();
vault.setLocation("local_manager");
vault.setAutoSplit(true);
JsonObject entry = new JsonObject();
entry.updateString("appName", "SchwabTrader");
entry.updateString("service", "OAuth2");
entry.updateString("username", "personal-desktop");
JsonObject tokens = new JsonObject();
try {
vault.getSecretJson(entry, tokens);
} catch (ChilkatException e) {
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 ("".equals(accessToken)) {
System.out.println("Run the Schwab authorization or refresh example to save an access token.");
return;
}
Http http = new Http();
http.setRequireSslCertVerify(true);
http.setAuthToken(accessToken);
// Supply one of these from your account picker. Leave both empty to list only.
String selectedEnding = "";
String selectedFullNumber = "";
HttpResponse resp = new HttpResponse();
try {
http.httpNoBody("GET", "https://api.schwabapi.com/trader/v1/accounts/accountNumbers", resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (resp.getStatusCode() != 200)) {
if (resp.getStatusCode() == 0) {
System.out.println("No HTTP response. Check connectivity.");
} else {
System.out.println("Schwab returned HTTP " + resp.getStatusCode());
if (resp.getStatusCode() == 401) {
System.out.println("Refresh the access token or repeat desktop authorization.");
}
}
return;
}
JsonArray accounts = new JsonArray();
try {
accounts.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Schwab returned unexpected JSON.");
return;
}
JsonObject row = new JsonObject();
StringBuilder number = new StringBuilder();
int matches = 0;
int i = 0;
String selectedHash = "";
while (i < accounts.getSize()) {
accounts.objectAt2(i, row);
String accountNumber = row.stringOf("accountNumber");
number.setString(accountNumber);
if (number.getLength() >= 4) {
int start = number.getLength() - 4;
String ending = number.getRange(start, 4, false);
System.out.println("Account ending in " + ending);
boolean matched = false;
if (!"".equals(selectedFullNumber)) {
if (accountNumber.equals(selectedFullNumber)) {
matched = true;
}
} else {
if (ending.equals(selectedEnding)) {
matched = true;
}
}
if (matched) {
matches = matches + 1;
selectedHash = row.stringOf("hashValue");
}
}
i = i + 1;
}
if ((matches != 1) || ("".equals(selectedHash))) {
System.out.println("Select exactly one account. For duplicate endings use the full-account input.");
return;
}
entry.updateString("service", "SelectedAccount");
JsonObject selection = new JsonObject();
selection.updateString("hashValue", selectedHash);
try {
vault.updateSecretJson(entry, selection);
} catch (ChilkatException e) {
System.out.println("Could not save the selected account.");
return;
}
System.out.println("Selected account saved.");
}
}