C++ Requires Chilkat v11.0.0+
C++
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 C++ Downloads
#include <CkSecrets.h>
#include <CkJsonObject.h>
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkJsonArray.h>
#include <CkStringBuilder.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkSecrets vault;
vault.put_Location("local_manager");
vault.put_AutoSplit(true);
CkJsonObject entry;
entry.UpdateString("appName","SchwabTrader");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","personal-desktop");
CkJsonObject tokens;
success = vault.GetSecretJson(entry,tokens);
if (success != true) {
std::cout << "Could not read the token record. Check the store and complete desktop authorization first." << "\r\n";
return;
}
const char *accessToken = tokens.stringOf("access_token");
if (accessToken == "") {
std::cout << "Run the Schwab authorization or refresh example to save an access token." << "\r\n";
return;
}
CkHttp http;
http.put_RequireSslCertVerify(true);
http.put_AuthToken(accessToken);
// Supply one of these from your account picker. Leave both empty to list only.
const char *selectedEnding = "";
const char *selectedFullNumber = "";
CkHttpResponse resp;
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) {
std::cout << "No HTTP response. Check connectivity." << "\r\n";
}
else {
std::cout << "Schwab returned HTTP " << resp.get_StatusCode() << "\r\n";
if (resp.get_StatusCode() == 401) {
std::cout << "Refresh the access token or repeat desktop authorization." << "\r\n";
}
}
return;
}
CkJsonArray accounts;
success = accounts.Load(resp.bodyStr());
if (success != true) {
std::cout << "Schwab returned unexpected JSON." << "\r\n";
return;
}
CkJsonObject row;
CkStringBuilder number;
int matches = 0;
int i = 0;
const char *selectedHash = "";
while ((i < accounts.get_Size())) {
accounts.ObjectAt2(i,row);
const char *accountNumber = row.stringOf("accountNumber");
number.SetString(accountNumber);
if (number.get_Length() >= 4) {
int start = number.get_Length() - 4;
const char *ending = number.getRange(start,4,false);
std::cout << "Account ending in " << ending << "\r\n";
bool 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 == "")) {
std::cout << "Select exactly one account. For duplicate endings use the full-account input." << "\r\n";
return;
}
entry.UpdateString("service","SelectedAccount");
CkJsonObject selection;
selection.UpdateString("hashValue",selectedHash);
success = vault.UpdateSecretJson(entry,selection);
if (success != true) {
std::cout << "Could not save the selected account." << "\r\n";
return;
}
std::cout << "Selected account saved." << "\r\n";
}