Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v9.5.0.62+

Facebook Download all Photos to Local Files

See more Facebook Examples

Demonstrates how to download all of one's Facebook photos to a local filesystem directory. This sample code keeps a local cache to avoid re-downloading the same photos twice. The program can be run again after a time, and it will download only photos that haven't yet been downloaded.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
fbCache: TChilkatCache;
oauth2: TChilkatOAuth2;
rest: TChilkatRest;
responseJson: WideString;
photoJson: TChilkatJsonObject;
saPhotoUrls: TChilkatStringTable;
sbPhotoIdPath: TChilkatStringBuilder;
json: TChilkatJsonObject;
i: Integer;
photoId: WideString;
imageUrl: WideString;
afterCursor: WideString;
numItems: Integer;
photoJsonStr: WideString;
imgUrlJson: TChilkatJsonObject;
http: TChilkatHttp;
numUrls: Integer;
urlJson: TChilkatJsonObject;
imageBytes: Array of Byte;
fac: TCkFileAccess;
sbImageUrl: TChilkatStringBuilder;
extension: WideString;
sbLocalFilePath: TChilkatStringBuilder;

begin
success := 0;

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

//  This example will use a local disk cache to avoid re-fetching the same
//  photo id after it's been fetched once.
fbCache := TChilkatCache.Create(Self);
//  The cache will use 1 level of 256 sub-directories.
fbCache.Level := 1;
//  Use a directory path that makes sense on your operating system..
fbCache.AddRoot('C:/fbCache');

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

//  There are two choices:  
//  We can choose to download the photos the person is tagged in or has uploaded
//  by setting type to "tagged" or "uploaded".
rest.AddQueryParam('type','uploaded');

//  To download all photos, we begin with an outer loop that iterates over
//  the list of photo nodes in pages.  Each page returned contains a list of 
//  photo node ids.  Each photo node id must be retrieved to get the download URL(s)
//  of the actual image.

//  I don't know the max limit for the number of records that can be downloaded at once.
rest.AddQueryParam('limit','100');

//  Get the 1st page of photos ids.
//  See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
responseJson := rest.FullRequestNoBody('GET','/v2.7/me/photos');
if (rest.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

photoJson := TChilkatJsonObject.Create(Self);
saPhotoUrls := TChilkatStringTable.Create(Self);
sbPhotoIdPath := TChilkatStringBuilder.Create(Self);

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

//  Get the "after" cursor.
afterCursor := json.StringOf('paging.cursors.after');
while json.LastMethodSuccess = 1 do
  begin

    Memo1.Lines.Add('-------------------');
    Memo1.Lines.Add('afterCursor = ' + afterCursor);

    //  For each photo id in this page...
    i := 0;
    numItems := json.SizeOfArray('data');
    while i < numItems do
      begin
        json.I := i;
        photoId := json.StringOf('data[i].id');
        Memo1.Lines.Add('photoId = ' + photoId);

        //  We need to fetch the JSON for this photo.  Check to see if it's in the local disk cache,
        //  and if not, then get it from Facebook.
        photoJsonStr := fbCache.FetchText(photoId);
        if (fbCache.LastMethodSuccess = 0) then
          begin
            //  It's not locally available, so get it from Facebook..
            sbPhotoIdPath.Clear();
            sbPhotoIdPath.Append('/v2.7/');
            sbPhotoIdPath.Append(photoId);

            rest.ClearAllQueryParams();
            rest.AddQueryParam('fields','id,album,images');

            Memo1.Lines.Add('Fetching photo node from Facebook...');

            //  This REST request will continue using the existing connection.
            //  If the connection was closed, it will automatically reconnect to send the request.
            photoJsonStr := rest.FullRequestNoBody('GET',sbPhotoIdPath.GetAsString());
            if (rest.LastMethodSuccess <> 1) then
              begin
                Memo1.Lines.Add(rest.LastErrorText);
                Exit;
              end;
            //  Add the photo JSON to the local cache.
            fbCache.SaveTextNoExpire(photoId,'',photoJsonStr);
          end;

        //  Parse the photo JSON and add the main photo download URL to saPhotoUrls
        //  There may be multiple URLs in the images array, but the 1st one is the largest and main photo URL.
        //  The others are smaller sizes of the same photo.
        photoJson.Load(photoJsonStr);
        imageUrl := photoJson.StringOf('images[0].source');
        if (photoJson.LastMethodSuccess = 1) then
          begin

            //  Actually, we'll add a small JSON document that contains both the image ID and the URL.
            imgUrlJson := TChilkatJsonObject.Create(Self);
            imgUrlJson.AppendString('id',photoId);
            imgUrlJson.AppendString('url',imageUrl);
            saPhotoUrls.Append(imgUrlJson.Emit());
            Memo1.Lines.Add('imageUrl = ' + imageUrl);
          end;

        i := i + 1;
      end;

    //  Prepare for getting the next page of photos ids.
    //  We can continue using the same REST object.
    //  If already connected, we'll continue using the existing connection.
    //  Otherwise, a new connection will automatically be made if needed.
    rest.ClearAllQueryParams();
    rest.AddQueryParam('type','uploaded');
    rest.AddQueryParam('limit','20');
    rest.AddQueryParam('after',afterCursor);

    //  Get the next page of photo ids.
    responseJson := rest.FullRequestNoBody('GET','/v2.7/me/photos');
    if (rest.LastMethodSuccess <> 1) then
      begin
        Memo1.Lines.Add(rest.LastErrorText);
        Exit;
      end;

    json.Load(responseJson);
    afterCursor := json.StringOf('paging.cursors.after');
  end;

Memo1.Lines.Add('No more pages of photos.');

//  Now iterate over the photo URLs and download each to a file.
//  We can use Chilkat HTTP.  No Facebook authorization (access token) is required to download
//  the photo once the URL is known.  
http := TChilkatHttp.Create(Self);

//  We'll cache the image data so that if run again, we don't re-download the same image again.
numUrls := saPhotoUrls.Count;
i := 0;
urlJson := TChilkatJsonObject.Create(Self);

fac := TCkFileAccess.Create(Self);

while i < numUrls do
  begin
    urlJson.Load(saPhotoUrls.StringAt(i));
    photoId := urlJson.StringOf('id');
    imageUrl := urlJson.StringOf('url');

    //  Check the local cache for the image data.
    //  Only download and save if not already cached.
    imageBytes := fbCache.FetchFromCache(imageUrl);
    if (fbCache.LastMethodSuccess = 0) then
      begin
        //   This photo needs to be downloaded.

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

        //  Let's form a filename..
        extension := '.jpg';
        if (sbImageUrl.Contains('.gif',0) = 1) then
          begin
            extension := '.gif';
          end;
        if (sbImageUrl.Contains('.png',0) = 1) then
          begin
            extension := '.png';
          end;
        if (sbImageUrl.Contains('.tiff',0) = 1) then
          begin
            extension := '.tiff';
          end;
        if (sbImageUrl.Contains('.bmp',0) = 1) then
          begin
            extension := '.bmp';
          end;

        sbLocalFilePath := TChilkatStringBuilder.Create(Self);
        sbLocalFilePath.Append('C:/Photos/facebook/uploaded/');
        sbLocalFilePath.Append(photoId);
        sbLocalFilePath.Append(extension);

        imageBytes := http.QuickGet(imageUrl);
        if (http.LastMethodSuccess <> 1) then
          begin
            Memo1.Lines.Add(http.LastErrorText);
            Exit;
          end;

        //  We've downloaded the photo image bytes into memory.
        //  Save it to the cache AND save it to the output file.
        fbCache.SaveToCacheNoExpire(imageUrl,'',imageBytes);
        fac.WriteEntireFile(sbLocalFilePath.GetAsString(),imageBytes);

        Memo1.Lines.Add('Downloaded to ' + sbLocalFilePath.GetAsString());
      end;

    i := i + 1;
  end;

Memo1.Lines.Add('Finished downloading all Facebook photos!');