Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Download Google Contact Photo
See more Google APIs Examples
Demonstrates how to download Google Contact's photo.Chilkat Pascal (Lazarus/Delphi) 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.StringBuilder,
Chilkat.AuthGoogle,
Chilkat.BinData,
Chilkat.JsonObject,
Chilkat.Rest;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
jsonToken: TJsonObject;
gAuth: TAuthGoogle;
rest: TRest;
bAutoReconnect: Boolean;
sbPath: TStringBuilder;
contactId: string;
numReplacements: Integer;
imageData: TBinData;
sbResponseBody: TStringBuilder;
sbContentType: TStringBuilder;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// --------------------------------------------------------------------------------------------------------
// Note: The code for setting up the Chilkat REST object and making the initial connection can be done once.
// Once connected, the REST object may be re-used for many REST API calls.
// (It's a good idea to put the connection setup code in a separate function/subroutine.)
// --------------------------------------------------------------------------------------------------------
// It is assumed we previously obtained an OAuth2 access token.
// This example loads the JSON access token file
// saved by this example: Get Google Contacts OAuth2 Access Token
jsonToken := TJsonObject.Create;
success := jsonToken.LoadFile('qa_data/tokens/googleContacts.json');
if (success <> True) then
begin
WriteLn('Failed to load googleContacts.json');
Exit;
end;
gAuth := TAuthGoogle.Create;
gAuth.AccessToken := jsonToken.StringOf('access_token');
rest := TRest.Create;
// Connect using TLS.
bAutoReconnect := True;
success := rest.Connect('www.google.com',443,True,bAutoReconnect);
// Provide the authentication credentials (i.e. the access token)
rest.SetAuthGoogle(gAuth);
// ----------------------------------------------
// OK, the REST connection setup is completed..
// ----------------------------------------------
// To get the photo, send the following:
// GET /m8/feeds/photos/media/default/contactId
rest.AddHeader('GData-Version','3.0');
sbPath := TStringBuilder.Create;
// Get the photo for the contact having contactId = "1ea2e4fe0ef24e09"
contactId := '1ea2e4fe0ef24e09';
sbPath.SetString('/m8/feeds/photos/media/default/{contactId}');
numReplacements := sbPath.Replace('{contactId}',contactId);
imageData := TBinData.Create;
success := rest.FullRequestNoBodyBd('GET',sbPath.GetAsString(),imageData);
if (success <> True) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
// A 404 response indicates the contact has no photo.
// (We could've first fetched the contact information, parsed out the
// photo etag, and then if no photo etag existed, we'd know the contact has no
// photo. Or... we can just try to download the photo and if a 404 is received,
// we know there's no photo. Much simpler.)
if (rest.ResponseStatusCode = 404) then
begin
WriteLn('This contact has no photo.');
Exit;
end;
// A successful response will have a status code equal to 200.
if (rest.ResponseStatusCode <> 200) then
begin
// If the response was not successful, then the response body
// does not contain image data. Instead it contains XML.
sbResponseBody := TStringBuilder.Create;
sbResponseBody.AppendBd(imageData,'utf-8',0,0);
WriteLn('response status code = ' + rest.ResponseStatusCode);
WriteLn('response status text = ' + rest.ResponseStatusText);
WriteLn('response header: ' + rest.ResponseHeader);
WriteLn('response body: ' + sbResponseBody.GetAsString());
WriteLn('request startline: ' + rest.LastRequestStartLine);
WriteLn('request header: ' + rest.LastRequestHeader);
Exit;
end;
// Examine the content-type in the response header so we know what file
// extension to use (.jpg, .png, etc.)
sbContentType := TStringBuilder.Create;
sbContentType.Append(rest.ResponseHdrByName('Content-Type'));
if (sbContentType.ContentsEqual('image/jpeg',False) = True) then
begin
imageData.WriteFile('qa_output/contact_photo.jpg');
end;
if (sbContentType.ContentsEqual('image/png',False) = True) then
begin
imageData.WriteFile('qa_output/contact_photo.png');
end;
WriteLn('Content-Type: ' + sbContentType.GetAsString());
WriteLn('Success.');
jsonToken.Free;
gAuth.Free;
rest.Free;
sbPath.Free;
imageData.Free;
sbResponseBody.Free;
sbContentType.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.