Sample code for 30+ languages & platforms
Delphi DLL

Download Google Contact Photo

See more Google APIs Examples

Demonstrates how to download Google Contact's photo.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, AuthGoogle, BinData, JsonObject, Rest;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
jsonToken: HCkJsonObject;
gAuth: HCkAuthGoogle;
rest: HCkRest;
bAutoReconnect: Boolean;
sbPath: HCkStringBuilder;
contactId: PWideChar;
numReplacements: Integer;
imageData: HCkBinData;
sbResponseBody: HCkStringBuilder;
sbContentType: HCkStringBuilder;

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 := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/googleContacts.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Failed to load googleContacts.json');
    Exit;
  end;

gAuth := CkAuthGoogle_Create();
CkAuthGoogle_putAccessToken(gAuth,CkJsonObject__stringOf(jsonToken,'access_token'));

rest := CkRest_Create();

// Connect using TLS.
bAutoReconnect := True;
success := CkRest_Connect(rest,'www.google.com',443,True,bAutoReconnect);

// Provide the authentication credentials (i.e. the access token)
CkRest_SetAuthGoogle(rest,gAuth);

// ----------------------------------------------
// OK, the REST connection setup is completed..
// ----------------------------------------------

// To get the photo, send the following:

// 	GET /m8/feeds/photos/media/default/contactId

CkRest_AddHeader(rest,'GData-Version','3.0');

sbPath := CkStringBuilder_Create();
// Get the photo for the contact having contactId = "1ea2e4fe0ef24e09"
contactId := '1ea2e4fe0ef24e09';
CkStringBuilder_SetString(sbPath,'/m8/feeds/photos/media/default/{contactId}');
numReplacements := CkStringBuilder_Replace(sbPath,'{contactId}',contactId);

imageData := CkBinData_Create();
success := CkRest_FullRequestNoBodyBd(rest,'GET',CkStringBuilder__getAsString(sbPath),imageData);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    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 (CkRest_getResponseStatusCode(rest) = 404) then
  begin
    Memo1.Lines.Add('This contact has no photo.');
    Exit;
  end;

// A successful response will have a status code equal to 200.
if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    // If the response was not successful, then the response body
    // does not contain image data.  Instead it contains XML.
    sbResponseBody := CkStringBuilder_Create();
    CkStringBuilder_AppendBd(sbResponseBody,imageData,'utf-8',0,0);

    Memo1.Lines.Add('response status code = ' + IntToStr(CkRest_getResponseStatusCode(rest)));
    Memo1.Lines.Add('response status text = ' + CkRest__responseStatusText(rest));
    Memo1.Lines.Add('response header: ' + CkRest__responseHeader(rest));
    Memo1.Lines.Add('response body: ' + CkStringBuilder__getAsString(sbResponseBody));
    Memo1.Lines.Add('request startline: ' + CkRest__lastRequestStartLine(rest));
    Memo1.Lines.Add('request header: ' + CkRest__lastRequestHeader(rest));
    Exit;
  end;

// Examine the content-type in the response header so we know what file
// extension to use (.jpg, .png, etc.)
sbContentType := CkStringBuilder_Create();
CkStringBuilder_Append(sbContentType,CkRest__responseHdrByName(rest,'Content-Type'));

if (CkStringBuilder_ContentsEqual(sbContentType,'image/jpeg',False) = True) then
  begin
    CkBinData_WriteFile(imageData,'qa_output/contact_photo.jpg');
  end;
if (CkStringBuilder_ContentsEqual(sbContentType,'image/png',False) = True) then
  begin
    CkBinData_WriteFile(imageData,'qa_output/contact_photo.png');
  end;

Memo1.Lines.Add('Content-Type: ' + CkStringBuilder__getAsString(sbContentType));
Memo1.Lines.Add('Success.');

CkJsonObject_Dispose(jsonToken);
CkAuthGoogle_Dispose(gAuth);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbPath);
CkBinData_Dispose(imageData);
    CkStringBuilder_Dispose(sbResponseBody);
CkStringBuilder_Dispose(sbContentType);

end;