Facebook API to Get Metadata of Page/Url (Python)

April 16, 2020

PURPOSE: I wanted to have server side (without user login) query of public facebook post

You need a Facebook Developer Account.

You could experiment with the API via Graph API Explorer

  • I am trying to build an backend API (without user login), so I select App Token

  • I try to query about a specific user of page (e.g WHO) by putting in the user name/id or page name/id, but it prompts this error: (#10) This endpoint requires the 'manage_pages' permission or the 'Page Public Content Access' feature or the 'Page Public Metadata Access' feature..

  • I try to query for a post by post id (for https://www.facebook.com/WHO/posts/3060781523967210, post id is 3060781523967210), but it prompts (#12) singular statuses API is deprecated for versions v2.4 and higher.

  • After some googling it seems I need to use the format of UserOrPageId_PostId. I use Find your Facebook ID (since I don’t have permission to query for a page or user) to find Facebook ID for https://www.facebook.com/WHO which is 154163327962392. I query for the post again using 154163327962392_3060781523967210 and it prompts this error: (#10) This endpoint requires the 'manage_pages' permission or the 'Page Public Content Access' feature.

I try to read more about manage-page and Page Public Content Access / Page Public Metadata Access, and it seems like we have to go through a set of stringent and time consuming verification process.

Goto Facebook Developer -> My Apps -> App Review -> Permission and Features to see what of verification process is required for each permission:

NOTE: I guess the days for an individual to do a quick and simple hacking via Facebook Graph API is over. Things are much simpler with Twitter API.

Python

The following is some sample Python code to perform query as per using Graph API Explorer.

NOTE: I didn’t test further as I didn’t bother to go through with the verification/review process to get the necessary permission.

Facebook SDK for Python seems to work, though the last release is v3.1.0 (Nov 6, 2018).

Install

pip install facebook-sdk

Code

import facebook

graph = facebook.GraphAPI(access_token=FACEBOOK_APP_ACCESS_TOKEN)

# get a post - https://www.facebook.com/WHO/posts/3060781523967210
# this will fail if your app doesn't have the necessary permission via reqvire process mentioned above
graph.get_object(id='154163327962392_3060781523967210', fields='id,created_time')

NOTE: Get FACEBOOK_APP_ACCESS_TOKEN from Access Token Tool

NOTE: Read Post /{post-id} documentation

NOTE: Refer to Facebook Oembed API to get content preview and date (without time) of a post.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.