Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Read selected headers for a page of Gmail messages
See more GMail REST API Examples
List up to five messages and fetch only From, Subject, and Date for each one. Request metadataHeaders once per header name. Read the returned headers by name because order and presence vary. This header preview leaves the MIME body on Gmail.
Authorize the application with https://www.googleapis.com/auth/gmail.metadata. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
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.StringTable,
Chilkat.StringBuilder,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
token: TJsonObject;
http: THttp;
resp: THttpResponse;
reply: TJsonObject;
ids: TStringTable;
count: Integer;
i: Integer;
url: TStringBuilder;
h: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token := TJsonObject.Create;
success := token.LoadFile('gmail-token.json');
if (success <> True) then
begin
WriteLn('Could not read gmail-token.json from the private working directory.');
Exit;
end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',True) = True)) then
begin
WriteLn('An OAuth access token is required.');
Exit;
end;
http := THttp.Create;
http.RequireSslCertVerify := True;
http.AuthToken := token.StringOf('access_token');
resp := THttpResponse.Create;
reply := TJsonObject.Create;
success := http.HttpNoBody('GET','https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5',resp);
if (resp.StatusCode = 0) then
begin
WriteLn('Gmail did not return an HTTP response.');
Exit;
end;
if ((success <> True) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
begin
WriteLn('Gmail HTTP ' + resp.StatusCode);
success := reply.Load(resp.BodyStr);
if (success = True) then
begin
WriteLn(reply.StringOf('error.message'));
end;
Exit;
end;
success := reply.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Expected a Gmail JSON response.');
Exit;
end;
ids := TStringTable.Create;
count := reply.SizeOfArray('messages');
i := 0;
while (i < count) do
begin
reply.I := i;
success := ids.Append(reply.StringOf('messages[i].id'));
if (success <> True) then
begin
WriteLn('Could not retain a message ID.');
Exit;
end;
i := i + 1;
end;
url := TStringBuilder.Create;
i := 0;
while (i < ids.Count) do
begin
url.SetString('https://gmail.googleapis.com/gmail/v1/users/me/messages/');
url.Append(ids.StringAt(i));
url.Append('?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date');
success := http.HttpNoBody('GET',url.GetAsString(),resp);
if (resp.StatusCode = 0) then
begin
WriteLn('Gmail did not return an HTTP response.');
Exit;
end;
if ((success <> True) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
begin
WriteLn('Gmail HTTP ' + resp.StatusCode);
success := reply.Load(resp.BodyStr);
if (success = True) then
begin
WriteLn(reply.StringOf('error.message'));
end;
Exit;
end;
success := reply.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Expected a Gmail JSON response.');
Exit;
end;
WriteLn('Message: ' + ids.StringAt(i));
h := 0;
while (h < reply.SizeOfArray('payload.headers')) do
begin
reply.I := h;
WriteLn(reply.StringOf('payload.headers[i].name') + ': ' + reply.StringOf('payload.headers[i].value'));
h := h + 1;
end;
i := i + 1;
end;
WriteLn('Messages inspected: ' + ids.Count);
token.Free;
http.Free;
resp.Free;
reply.Free;
ids.Free;
url.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.