Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Delphi DLL Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Delphi DLL) Download Google Contact Photo

Demonstrates how to download Google Contact's photo.

Chilkat for Delphi Downloads

Chilkat non-ActiveX DLL for Delphi

Chilkat ActiveX DLL for Delphi

* The examples here use the non-ActiveX 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
jsonToken: HCkJsonObject;
success: Boolean;
gAuth: HCkAuthGoogle;
rest: HCkRest;
bAutoReconnect: Boolean;
sbPath: HCkStringBuilder;
contactId: PWideChar;
numReplacements: Integer;
imageData: HCkBinData;
sbResponseBody: HCkStringBuilder;
sbContentType: HCkStringBuilder;

begin
// 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;

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.