Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
accessToken: PWideChar;
http: HCkHttp;
selectedEnding: PWideChar;
selectedFullNumber: PWideChar;
resp: HCkHttpResponse;
accounts: HCkJsonArray;
row: HCkJsonObject;
number: HCkStringBuilder;
matches: Integer;
i: Integer;
selectedHash: PWideChar;
accountNumber: PWideChar;
start: Integer;
ending: PWideChar;
matched: Boolean;
selection: HCkJsonObject;
begin
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) then
begin
Memo1.Lines.Add('Could not read the token record. Check the store and complete desktop authorization first.');
Exit;
end;
accessToken := CkJsonObject__stringOf(tokens,'access_token');
if (accessToken = '') then
begin
Memo1.Lines.Add('Run the Schwab authorization or refresh example to save an access token.');
Exit;
end;
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) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
begin
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add('No HTTP response. Check connectivity.');
end
else
begin
Memo1.Lines.Add('Schwab returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
if (CkHttpResponse_getStatusCode(resp) = 401) then
begin
Memo1.Lines.Add('Refresh the access token or repeat desktop authorization.');
end;
end;
Exit;
end;
accounts := CkJsonArray_Create();
success := CkJsonArray_Load(accounts,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Schwab returned unexpected JSON.');
Exit;
end;
row := CkJsonObject_Create();
number := CkStringBuilder_Create();
matches := 0;
i := 0;
selectedHash := '';
while (i < CkJsonArray_getSize(accounts)) do
begin
CkJsonArray_ObjectAt2(accounts,i,row);
accountNumber := CkJsonObject__stringOf(row,'accountNumber');
CkStringBuilder_SetString(number,accountNumber);
if (CkStringBuilder_getLength(number) >= 4) then
begin
start := CkStringBuilder_getLength(number) - 4;
ending := CkStringBuilder__getRange(number,start,4,False);
Memo1.Lines.Add('Account ending in ' + ending);
matched := False;
if (selectedFullNumber <> '') then
begin
if (accountNumber = selectedFullNumber) then
begin
matched := True;
end;
end
else
begin
if (ending = selectedEnding) then
begin
matched := True;
end;
end;
if (matched = True) then
begin
matches := matches + 1;
selectedHash := CkJsonObject__stringOf(row,'hashValue');
end;
end;
i := i + 1;
end;
if ((matches <> 1) or (selectedHash = '')) then
begin
Memo1.Lines.Add('Select exactly one account. For duplicate endings use the full-account input.');
Exit;
end;
CkJsonObject_UpdateString(entry,'service','SelectedAccount');
selection := CkJsonObject_Create();
CkJsonObject_UpdateString(selection,'hashValue',selectedHash);
success := CkSecrets_UpdateSecretJson(vault,entry,selection);
if (success <> True) then
begin
Memo1.Lines.Add('Could not save the selected account.');
Exit;
end;
Memo1.Lines.Add('Selected account saved.');
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);