Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.Http,
  Chilkat.OAuth2,
  Chilkat.Rest,
  Chilkat.StringBuilder,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  oauth2: TOAuth2;
  rest: TRest;
  photoId: string;
  sbPath: TStringBuilder;
  responseJson: string;
  json: TJsonObject;
  imageUrl: string;
  sbImageUrl: TStringBuilder;
  sbToPath: TStringBuilder;
  bCaseSensitive: Boolean;
  http: THttp;

begin
  success := False;

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

  rest := TRest.Create;

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

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

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

  sbPath := TStringBuilder.Create;
  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 <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  json := TJsonObject.Create;
  json.EmitCompact := False;
  json.Load(responseJson);

  //  Show the JSON in human-readable format.
  WriteLn(json.Emit());

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

  sbImageUrl := TStringBuilder.Create;
  sbImageUrl.Append(imageUrl);

  //  Build the output local file path.
  sbToPath := TStringBuilder.Create;
  sbToPath.Append('qa_output/fb');
  sbToPath.Append(json.StringOf('id'));
  bCaseSensitive := False;
  if (sbImageUrl.Contains('.jpg',bCaseSensitive) = True) then
    begin
      sbToPath.Append('.jpg');
    end
  else
    begin
      sbToPath.Append('.png');
    end;
  WriteLn('Downloading to ' + sbToPath.GetAsString());

  //  Download using Chilkat HTTP.
  http := THttp.Create;
  success := http.Download(imageUrl,sbToPath.GetAsString());
  if (success <> True) then
    begin
      WriteLn(http.LastErrorText);
    end
  else
    begin
      WriteLn('Downloaded.');
    end;


  oauth2.Free;
  rest.Free;
  sbPath.Free;
  json.Free;
  sbImageUrl.Free;
  sbToPath.Free;
  http.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.