Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.JsonArray,
Chilkat.HttpResponse,
Chilkat.StringBuilder,
Chilkat.JsonObject,
Chilkat.Secrets,
Chilkat.Http;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
vault: TSecrets;
entry: TJsonObject;
tokens: TJsonObject;
accessToken: string;
http: THttp;
selectedEnding: string;
selectedFullNumber: string;
resp: THttpResponse;
accounts: TJsonArray;
row: TJsonObject;
number: TStringBuilder;
matches: Integer;
i: Integer;
selectedHash: string;
accountNumber: string;
start: Integer;
ending: string;
matched: Boolean;
selection: TJsonObject;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
vault := TSecrets.Create;
vault.Location := 'local_manager';
vault.AutoSplit := True;
entry := TJsonObject.Create;
entry.UpdateString('appName','SchwabTrader');
entry.UpdateString('service','OAuth2');
entry.UpdateString('username','personal-desktop');
tokens := TJsonObject.Create;
success := vault.GetSecretJson(entry,tokens);
if (success <> True) then
begin
WriteLn('Could not read the token record. Check the store and complete desktop authorization first.');
Exit;
end;
accessToken := tokens.StringOf('access_token');
if (accessToken = '') then
begin
WriteLn('Run the Schwab authorization or refresh example to save an access token.');
Exit;
end;
http := THttp.Create;
http.RequireSslCertVerify := True;
http.AuthToken := accessToken;
// Supply one of these from your account picker. Leave both empty to list only.
selectedEnding := '';
selectedFullNumber := '';
resp := THttpResponse.Create;
success := http.HttpNoBody('GET','https://api.schwabapi.com/trader/v1/accounts/accountNumbers',resp);
if ((success <> True) or (resp.StatusCode <> 200)) then
begin
if (resp.StatusCode = 0) then
begin
WriteLn('No HTTP response. Check connectivity.');
end
else
begin
WriteLn('Schwab returned HTTP ' + resp.StatusCode);
if (resp.StatusCode = 401) then
begin
WriteLn('Refresh the access token or repeat desktop authorization.');
end;
end;
Exit;
end;
accounts := TJsonArray.Create;
success := accounts.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Schwab returned unexpected JSON.');
Exit;
end;
row := TJsonObject.Create;
number := TStringBuilder.Create;
matches := 0;
i := 0;
selectedHash := '';
while (i < accounts.Size) do
begin
accounts.ObjectAt2(i,row);
accountNumber := row.StringOf('accountNumber');
number.SetString(accountNumber);
if (number.Length >= 4) then
begin
start := number.Length - 4;
ending := number.GetRange(start,4,False);
WriteLn('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 := row.StringOf('hashValue');
end;
end;
i := i + 1;
end;
if ((matches <> 1) or (selectedHash = '')) then
begin
WriteLn('Select exactly one account. For duplicate endings use the full-account input.');
Exit;
end;
entry.UpdateString('service','SelectedAccount');
selection := TJsonObject.Create;
selection.UpdateString('hashValue',selectedHash);
success := vault.UpdateSecretJson(entry,selection);
if (success <> True) then
begin
WriteLn('Could not save the selected account.');
Exit;
end;
WriteLn('Selected account saved.');
vault.Free;
entry.Free;
tokens.Free;
http.Free;
resp.Free;
accounts.Free;
row.Free;
number.Free;
selection.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.