Sample code for 30+ languages & platforms
Delphi ActiveX

Download Photo to a File

See more Facebook Examples

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

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
oauth2: TChilkatOAuth2;
rest: TChilkatRest;
photoId: WideString;
sbPath: TChilkatStringBuilder;
responseJson: WideString;
json: TChilkatJsonObject;
imageUrl: WideString;
sbImageUrl: TChilkatStringBuilder;
sbToPath: TChilkatStringBuilder;
bCaseSensitive: Integer;
http: TChilkatHttp;

begin
success := 0;

// 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 := TChilkatOAuth2.Create(Self);
oauth2.AccessToken := 'FACEBOOK-ACCESS-TOKEN';

rest := TChilkatRest.Create(Self);

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

// Provide the authentication credentials (i.e. the access key)
rest.SetAuthOAuth2(oauth2.ControlInterface);

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

sbPath := TChilkatStringBuilder.Create(Self);
sbPath.Append('/v2.7/');
sbPath.Append(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/
rest.AddQueryParam('fields','id,album,images');

responseJson := rest.FullRequestNoBody('GET',sbPath.GetAsString());
if (rest.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

json := TChilkatJsonObject.Create(Self);
json.EmitCompact := 0;
json.Load(responseJson);

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

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

sbImageUrl := TChilkatStringBuilder.Create(Self);
sbImageUrl.Append(imageUrl);

// Build the output local file path.
sbToPath := TChilkatStringBuilder.Create(Self);
sbToPath.Append('qa_output/fb');
sbToPath.Append(json.StringOf('id'));
bCaseSensitive := 0;
if (sbImageUrl.Contains('.jpg',bCaseSensitive) = 1) then
  begin
    sbToPath.Append('.jpg');
  end
else
  begin
    sbToPath.Append('.png');
  end;
Memo1.Lines.Add('Downloading to ' + sbToPath.GetAsString());

// Download using Chilkat HTTP.
http := TChilkatHttp.Create(Self);
success := http.Download(imageUrl,sbToPath.GetAsString());
if (success <> 1) then
  begin
    Memo1.Lines.Add(http.LastErrorText);
  end
else
  begin
    Memo1.Lines.Add('Downloaded.');
  end;
end;