Unicode C Requires Chilkat v11.0.0+
Unicode 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 Unicode C Downloads
#include <C_CkSecretsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonArrayW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSecretsW vault;
HCkJsonObjectW entry;
HCkJsonObjectW tokens;
const wchar_t *accessToken;
HCkHttpW http;
const wchar_t *selectedEnding;
const wchar_t *selectedFullNumber;
HCkHttpResponseW resp;
HCkJsonArrayW accounts;
HCkJsonObjectW row;
HCkStringBuilderW number;
int matches;
int i;
const wchar_t *selectedHash;
const wchar_t *accountNumber;
int start;
const wchar_t *ending;
BOOL matched;
HCkJsonObjectW selection;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
vault = CkSecretsW_Create();
CkSecretsW_putLocation(vault,L"local_manager");
CkSecretsW_putAutoSplit(vault,TRUE);
entry = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(entry,L"appName",L"SchwabTrader");
CkJsonObjectW_UpdateString(entry,L"service",L"OAuth2");
CkJsonObjectW_UpdateString(entry,L"username",L"personal-desktop");
tokens = CkJsonObjectW_Create();
success = CkSecretsW_GetSecretJson(vault,entry,tokens);
if (success != TRUE) {
wprintf(L"Could not read the token record. Check the store and complete desktop authorization first.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
return;
}
accessToken = CkJsonObjectW_stringOf(tokens,L"access_token");
if (accessToken == L"") {
wprintf(L"Run the Schwab authorization or refresh example to save an access token.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
return;
}
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
CkHttpW_putAuthToken(http,accessToken);
// Supply one of these from your account picker. Leave both empty to list only.
selectedEnding = L"";
selectedFullNumber = L"";
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpNoBody(http,L"GET",L"https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp);
if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) != 200)) {
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"No HTTP response. Check connectivity.\n");
}
else {
wprintf(L"Schwab returned HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
if (CkHttpResponseW_getStatusCode(resp) == 401) {
wprintf(L"Refresh the access token or repeat desktop authorization.\n");
}
}
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
return;
}
accounts = CkJsonArrayW_Create();
success = CkJsonArrayW_Load(accounts,CkHttpResponseW_bodyStr(resp));
if (success != TRUE) {
wprintf(L"Schwab returned unexpected JSON.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonArrayW_Dispose(accounts);
return;
}
row = CkJsonObjectW_Create();
number = CkStringBuilderW_Create();
matches = 0;
i = 0;
selectedHash = L"";
while ((i < CkJsonArrayW_getSize(accounts))) {
CkJsonArrayW_ObjectAt2(accounts,i,row);
accountNumber = CkJsonObjectW_stringOf(row,L"accountNumber");
CkStringBuilderW_SetString(number,accountNumber);
if (CkStringBuilderW_getLength(number) >= 4) {
start = CkStringBuilderW_getLength(number) - 4;
ending = CkStringBuilderW_getRange(number,start,4,FALSE);
wprintf(L"Account ending in %s\n",ending);
matched = FALSE;
if (selectedFullNumber != L"") {
if (accountNumber == selectedFullNumber) {
matched = TRUE;
}
}
else {
if (ending == selectedEnding) {
matched = TRUE;
}
}
if (matched == TRUE) {
matches = matches + 1;
selectedHash = CkJsonObjectW_stringOf(row,L"hashValue");
}
}
i = i + 1;
}
if ((matches != 1) || (selectedHash == L"")) {
wprintf(L"Select exactly one account. For duplicate endings use the full-account input.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonArrayW_Dispose(accounts);
CkJsonObjectW_Dispose(row);
CkStringBuilderW_Dispose(number);
return;
}
CkJsonObjectW_UpdateString(entry,L"service",L"SelectedAccount");
selection = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(selection,L"hashValue",selectedHash);
success = CkSecretsW_UpdateSecretJson(vault,entry,selection);
if (success != TRUE) {
wprintf(L"Could not save the selected account.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonArrayW_Dispose(accounts);
CkJsonObjectW_Dispose(row);
CkStringBuilderW_Dispose(number);
CkJsonObjectW_Dispose(selection);
return;
}
wprintf(L"Selected account saved.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonArrayW_Dispose(accounts);
CkJsonObjectW_Dispose(row);
CkStringBuilderW_Dispose(number);
CkJsonObjectW_Dispose(selection);
}