Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Delphi DLL Web API Examples

Primary Categories

AWS Translate
Activix CRM
Adyen
Amazon MWS
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bluzone
CardConnect
ClickBank
Clickatell
Cloudfare
DocuSign
ETrade
Etsy
Facebook
GeoOp
GetHarvest
Jira
MYOB

Magento
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Okta OAuth/OIDC
OneLogin OIDC
PayPal
Peoplevox
Populi
QuickBooks
SOAP finkok.com
Shippo
Shopify
Stripe
SugarCRM
Trello
Twitter
VoiceBase
Vonage
Walmart
Walmart v3
Xero
eBay
effectconnect

 

 

 

(Delphi DLL) Download Photo to a File

Assuming we have the ID of a Photo, this example demonstrates how to download the photo image data to a file.

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, Http, OAuth2, Rest, StringBuilder, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
oauth2: HCkOAuth2;
rest: HCkRest;
success: Boolean;
photoId: PWideChar;
sbPath: HCkStringBuilder;
responseJson: PWideChar;
json: HCkJsonObject;
imageUrl: PWideChar;
sbImageUrl: HCkStringBuilder;
sbToPath: HCkStringBuilder;
bCaseSensitive: Boolean;
http: HCkHttp;

begin
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// This example assumes a previously obtained an access token
oauth2 := CkOAuth2_Create();
CkOAuth2_putAccessToken(oauth2,'FACEBOOK-ACCESS-TOKEN');

rest := CkRest_Create();

// Connect to Facebook...
success := CkRest_Connect(rest,'graph.facebook.com',443,True,True);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

// Provide the authentication credentials (i.e. the access key)
CkRest_SetAuthOAuth2(rest,oauth2);

// Assumes we've already obtained a Photo ID.
photoId := '10210199026347451';

sbPath := CkStringBuilder_Create();
CkStringBuilder_Append(sbPath,'/v2.7/');
CkStringBuilder_Append(sbPath,photoId);

// First we're going to get the photo informaton so we can get the URL of the image file data.
// Select the fields we want.
// See https://developers.facebook.com/docs/graph-api/reference/photo/
CkRest_AddQueryParam(rest,'fields','id,album,images');

responseJson := CkRest__fullRequestNoBody(rest,'GET',CkStringBuilder__getAsString(sbPath));
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

json := CkJsonObject_Create();
CkJsonObject_putEmitCompact(json,False);
CkJsonObject_Load(json,responseJson);

// Show the JSON in human-readable format.
Memo1.Lines.Add(CkJsonObject__emit(json));

// Get the image URL.
imageUrl := CkJsonObject__stringOf(json,'images[0].source');
Memo1.Lines.Add('Downloading from ' + imageUrl);

sbImageUrl := CkStringBuilder_Create();
CkStringBuilder_Append(sbImageUrl,imageUrl);

// Build the output local file path.
sbToPath := CkStringBuilder_Create();
CkStringBuilder_Append(sbToPath,'qa_output/fb');
CkStringBuilder_Append(sbToPath,CkJsonObject__stringOf(json,'id'));
bCaseSensitive := False;
if (CkStringBuilder_Contains(sbImageUrl,'.jpg',bCaseSensitive) = True) then
  begin
    CkStringBuilder_Append(sbToPath,'.jpg');
  end
else
  begin
    CkStringBuilder_Append(sbToPath,'.png');
  end;
Memo1.Lines.Add('Downloading to ' + CkStringBuilder__getAsString(sbToPath));

// Download using Chilkat HTTP.
http := CkHttp_Create();
success := CkHttp_Download(http,imageUrl,CkStringBuilder__getAsString(sbToPath));
if (success <> True) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
  end
else
  begin
    Memo1.Lines.Add('Downloaded.');
  end;

CkOAuth2_Dispose(oauth2);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbPath);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbImageUrl);
CkStringBuilder_Dispose(sbToPath);
CkHttp_Dispose(http);

end;

 

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