Search code examples
facebookapiphotofacebook-page

Get all photos of a page using Facebook API


I am new to Facebook API but I have to get all photos from a Facebook page. I managed to get a graph api query that returns all pictures of only the first ever created album on the page which is the profile picture album. For example:

https://graph.facebook.com/227843870675663/photos

But I need to do the same for all other existing albums in the page. Any pointers are really appreciated.


Solution

  • Try this and let me know how it goes for you. I've successfully pulled all photos from a Facebook page.

    1. Ensure that you are an Admin of the Facebook Page.

    2. Go to: http://developers.facebook.com/tools/explorer

    3. In the API Navigator, you can access /me to bring up the basic information of yourself.

    4. Try typing in /me/accounts to see if you can see anything. It should give you an error.

    5. You need to enable certain permissions to make this Graph query. Choose the relevant permissions in the right sidebar. According to this doc you need the pages_show_list and pages_read_engagement permissions for this query.

    6. Click on "Get Access Token" which will give you a new token with the updated permissions. You may get a popup window where you have to confirm which page(s) you would like this access token to be able to see.

    7. Now try /me/accounts again. You should see a list of pages that you enabled access for, inside the viewing window.

    8. Find the Facebook Page you want to query, and click on the "id" field. This ID is needed for making any queries related to this page.

    9. Next, on the left window, you can see "Node: " and a + sign. Click on the + sign to see what options you have.

    10. Click on the + sign and scroll down to "connections" and select "Albums"

    11. The child-level, select "Photos"

    12. The "Photos" child-level, select "source"

    13. Now click "Submit" on the right hand side.

      If the above doesn't work, you can try copying the following URL into the API explorer:

      YOUR_PAGE_ID/?fields=albums.fields(photos.fields(source))
      

      You will see a JSON returned with the URL of some/all of the photos in your selected Facebook Page.

    14. Click "Get Code" and it will show you the Javascript or raw URL used to access this information.

      The "cURL" tab will show the URL which will look something like this:

      https://graph.facebook.com/v15.0/YOUR_PAGE_ID?fields=albums.fields(photos.fields(source))&access_token=YOUR_ACCESS_TOKEN_HERE
      

      You can copy that URL and plug it into your browser. You should see a JSON of all your photos, and each photo's corresponding URL on Facebook's CDN.

    Getting photos posted to the feed, not just photo albums

    To do that, you need to run a slightly different query:

    https://graph.facebook.com/v15.0/YOUR_PAGE_ID/feed?fields=attachments
    

    Getting high resolution imagery

    Many of the image URLs provided by the queries above will only provide resolutions up to 720px. If you want higher resolution images, then you need to find the target.id for each subattachment, and perform a separate graph query for each image to obtain the other resolutions. It looks like this:

    https://graph.facebook.com/v15.0/YOUR_IMAGE_ID?fields=images
    

    Watch out for expiring tokens

    The token generated above will only work for approximately 1-2 hours. If you want a longer lasting token, follow the steps in this post here.