JavaScript Requires Chilkat v11.0.0+
JavaScript
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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
var vault = new CkSecrets();
vault.Location = "local_manager";
vault.AutoSplit = true;
var entry = new CkJsonObject();
entry.UpdateString("appName","SchwabTrader");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","personal-desktop");
var tokens = new CkJsonObject();
success = vault.GetSecretJson(entry,tokens);
if (success !== true) {
console.log("Could not read the token record. Check the store and complete desktop authorization first.");
return;
}
var accessToken = tokens.StringOf("access_token");
if (accessToken == "") {
console.log("Run the Schwab authorization or refresh example to save an access token.");
return;
}
var http = new CkHttp();
http.RequireSslCertVerify = true;
http.AuthToken = accessToken;
// Supply one of these from your account picker. Leave both empty to list only.
var selectedEnding = "";
var selectedFullNumber = "";
var resp = new CkHttpResponse();
success = http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp);
if ((success !== true) || (resp.StatusCode !== 200)) {
if (resp.StatusCode == 0) {
console.log("No HTTP response. Check connectivity.");
}
else {
console.log("Schwab returned HTTP " + resp.StatusCode);
if (resp.StatusCode == 401) {
console.log("Refresh the access token or repeat desktop authorization.");
}
}
return;
}
var accounts = new CkJsonArray();
success = accounts.Load(resp.BodyStr);
if (success !== true) {
console.log("Schwab returned unexpected JSON.");
return;
}
var row = new CkJsonObject();
var number = new CkStringBuilder();
var matches = 0;
var i = 0;
var selectedHash = "";
while ((i < accounts.Size)) {
accounts.ObjectAt2(i,row);
var accountNumber = row.StringOf("accountNumber");
number.SetString(accountNumber);
if (number.Length >= 4) {
var start = number.Length - 4;
var ending = number.GetRange(start,4,false);
console.log("Account ending in " + ending);
var 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 == "")) {
console.log("Select exactly one account. For duplicate endings use the full-account input.");
return;
}
entry.UpdateString("service","SelectedAccount");
var selection = new CkJsonObject();
selection.UpdateString("hashValue",selectedHash);
success = vault.UpdateSecretJson(entry,selection);
if (success !== true) {
console.log("Could not save the selected account.");
return;
}
console.log("Selected account saved.");