Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Constant Contact V3: Find a contact by email address
See more Constant Contact Examples
Look up an exact email address across contact states, including deleted contacts. Pass the address as a query parameter so a literal plus sign survives URL encoding. Read the stored contact ID, email permission, and deletion marker. An empty contacts array means no match. Authorize the app first and use the same credential store.
Constant Contact developer guide
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.Http,
Chilkat.Secrets,
Chilkat.HttpRequest,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
vault: TSecrets;
entry: TJsonObject;
tokens: TJsonObject;
loaded: Boolean;
http: THttp;
resp: THttpResponse;
query: THttpRequest;
reply: TJsonObject;
count: Integer;
i: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
vault := TSecrets.Create;
vault.Location := 'local_manager';
vault.AutoSplit := True;
entry := TJsonObject.Create;
entry.UpdateString('appName','ConstantContactV3');
entry.UpdateString('service','OAuth2');
entry.UpdateString('username','my-account');
tokens := TJsonObject.Create;
loaded := vault.GetSecretJson(entry,tokens);
if (loaded <> True) then
begin
WriteLn('Run the desktop authorization example using this account and store.');
Exit;
end;
if (tokens.JsonTypeOf('access_token') <> 1) then
begin
WriteLn('The saved authorization has no access token.');
Exit;
end;
http := THttp.Create;
http.RequireSslCertVerify := True;
http.AuthToken := tokens.StringOf('access_token');
resp := THttpResponse.Create;
query := THttpRequest.Create;
query.HttpVerb := 'GET';
query.AddParam('email','reader+newsletter@example.com');
query.AddParam('status','all');
success := http.HttpReq('https://api.cc.email/v3/contacts',query,resp);
if ((success <> True) or (resp.StatusCode <> 200)) then
begin
WriteLn('Expected HTTP 200. Received ' + resp.StatusCode);
if (resp.StatusCode = 401) then
begin
WriteLn('Refresh the saved access token or authorize again.');
end;
Exit;
end;
reply := TJsonObject.Create;
success := reply.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Expected a JSON response.');
Exit;
end;
count := reply.SizeOfArray('contacts');
if (count < 0) then
begin
WriteLn('The reply contained no contacts array.');
Exit;
end;
if (count = 0) then
begin
WriteLn('No contact has that email address.');
Exit;
end;
i := 0;
while (i < count) do
begin
reply.I := i;
WriteLn('Contact ID: ' + reply.StringOf('contacts[i].contact_id'));
WriteLn('Email permission: ' + reply.StringOf('contacts[i].email_address.permission_to_send'));
if (reply.JsonTypeOf('contacts[i].deleted_at') = 1) then
begin
WriteLn('Deleted at: ' + reply.StringOf('contacts[i].deleted_at'));
end;
i := i + 1;
end;
vault.Free;
entry.Free;
tokens.Free;
http.Free;
resp.Free;
query.Free;
reply.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.