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 <C_CkSecrets.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonArray.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkSecrets vault;
HCkJsonObject entry;
HCkJsonObject tokens;
const char *accessToken;
HCkHttp http;
const char *selectedEnding;
const char *selectedFullNumber;
HCkHttpResponse resp;
HCkJsonArray accounts;
HCkJsonObject row;
HCkStringBuilder number;
int matches;
int i;
const char *selectedHash;
const char *accountNumber;
int start;
const char *ending;
BOOL matched;
HCkJsonObject selection;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
vault = CkSecrets_Create();
CkSecrets_putLocation(vault,"local_manager");
CkSecrets_putAutoSplit(vault,TRUE);
entry = CkJsonObject_Create();
CkJsonObject_UpdateString(entry,"appName","SchwabTrader");
CkJsonObject_UpdateString(entry,"service","OAuth2");
CkJsonObject_UpdateString(entry,"username","personal-desktop");
tokens = CkJsonObject_Create();
success = CkSecrets_GetSecretJson(vault,entry,tokens);
if (success != TRUE) {
printf("Could not read the token record. Check the store and complete desktop authorization first.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
return;
}
accessToken = CkJsonObject_stringOf(tokens,"access_token");
if (accessToken == "") {
printf("Run the Schwab authorization or refresh example to save an access token.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
return;
}
http = CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,TRUE);
CkHttp_putAuthToken(http,accessToken);
// Supply one of these from your account picker. Leave both empty to list only.
selectedEnding = "";
selectedFullNumber = "";
resp = CkHttpResponse_Create();
success = CkHttp_HttpNoBody(http,"GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp);
if ((success != TRUE) || (CkHttpResponse_getStatusCode(resp) != 200)) {
if (CkHttpResponse_getStatusCode(resp) == 0) {
printf("No HTTP response. Check connectivity.\n");
}
else {
printf("Schwab returned HTTP %d\n",CkHttpResponse_getStatusCode(resp));
if (CkHttpResponse_getStatusCode(resp) == 401) {
printf("Refresh the access token or repeat desktop authorization.\n");
}
}
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
return;
}
accounts = CkJsonArray_Create();
success = CkJsonArray_Load(accounts,CkHttpResponse_bodyStr(resp));
if (success != TRUE) {
printf("Schwab returned unexpected JSON.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonArray_Dispose(accounts);
return;
}
row = CkJsonObject_Create();
number = CkStringBuilder_Create();
matches = 0;
i = 0;
selectedHash = "";
while ((i < CkJsonArray_getSize(accounts))) {
CkJsonArray_ObjectAt2(accounts,i,row);
accountNumber = CkJsonObject_stringOf(row,"accountNumber");
CkStringBuilder_SetString(number,accountNumber);
if (CkStringBuilder_getLength(number) >= 4) {
start = CkStringBuilder_getLength(number) - 4;
ending = CkStringBuilder_getRange(number,start,4,FALSE);
printf("Account ending in %s\n",ending);
matched = FALSE;
if (selectedFullNumber != "") {
if (accountNumber == selectedFullNumber) {
matched = TRUE;
}
}
else {
if (ending == selectedEnding) {
matched = TRUE;
}
}
if (matched == TRUE) {
matches = matches + 1;
selectedHash = CkJsonObject_stringOf(row,"hashValue");
}
}
i = i + 1;
}
if ((matches != 1) || (selectedHash == "")) {
printf("Select exactly one account. For duplicate endings use the full-account input.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonArray_Dispose(accounts);
CkJsonObject_Dispose(row);
CkStringBuilder_Dispose(number);
return;
}
CkJsonObject_UpdateString(entry,"service","SelectedAccount");
selection = CkJsonObject_Create();
CkJsonObject_UpdateString(selection,"hashValue",selectedHash);
success = CkSecrets_UpdateSecretJson(vault,entry,selection);
if (success != TRUE) {
printf("Could not save the selected account.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonArray_Dispose(accounts);
CkJsonObject_Dispose(row);
CkStringBuilder_Dispose(number);
CkJsonObject_Dispose(selection);
return;
}
printf("Selected account saved.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonArray_Dispose(accounts);
CkJsonObject_Dispose(row);
CkStringBuilder_Dispose(number);
CkJsonObject_Dispose(selection);
}