PyFacebook

Language: Python

Web

PyFacebook was created to simplify working with the Facebook Graph API in Python. It abstracts the complexities of HTTP requests, authentication, and response handling, enabling developers to build social media management tools, analytics applications, and automation scripts with ease.

PyFacebook is a Python SDK for interacting with the Facebook Graph API. It allows developers to manage Facebook pages, post content, retrieve insights, and handle user and page data programmatically.

Installation

pip: pip install pyfacebook
conda: conda install -c conda-forge pyfacebook

Usage

PyFacebook allows you to authenticate with Facebook, send requests to the Graph API, manage pages, retrieve posts, comments, insights, and more. It supports both user and page access tokens and provides Pythonic interfaces for various endpoints.

Initializing the client

from pyfacebook import GraphAPI
api = GraphAPI(app_id='YOUR_APP_ID', app_secret='YOUR_APP_SECRET', short_token='USER_ACCESS_TOKEN')

Creates a PyFacebook client instance using your app credentials and a user access token.

Getting user profile

profile = api.get_user('me')
print(profile)

Retrieves the authenticated user's profile information.

Posting to a page

api.post_page_feed(page_id='PAGE_ID', message='Hello from PyFacebook!')

Posts a message to a Facebook page using a page access token.

Retrieving page insights

insights = api.get_page_insights(page_id='PAGE_ID', metric=['page_impressions','page_engaged_users'])
print(insights)

Fetches analytics data (like impressions and engagement) for a specific Facebook page.

Fetching comments on a post

comments = api.get_post_comments(post_id='POST_ID')
for comment in comments['data']:
    print(comment['from']['name'], comment['message'])

Retrieves comments from a specific post and prints the author and message.

Handling pagination

posts = api.get_page_feed(page_id='PAGE_ID')
while 'paging' in posts and 'next' in posts['paging']:
    for post in posts['data']:
        print(post['message'])
    posts = api.get_next_page(posts)

Iterates through all posts on a page using pagination.

Updating a post

api.update_post(post_id='POST_ID', message='Updated message')

Updates the message of an existing post on a page.

Deleting a post

api.delete_post(post_id='POST_ID')

Deletes a specific post from a page.

Error Handling

GraphAPIError: Catch exceptions returned by the API and inspect error messages for troubleshooting.
OAuthException: Occurs when access tokens are invalid or expired. Refresh tokens or generate new ones.
HTTPError: Check network connectivity and API endpoint correctness.

Best Practices

Keep app secrets and access tokens secure using environment variables.

Use page access tokens for page management and user access tokens for user data.

Handle rate limits and exceptions gracefully with try/except blocks.

Paginate through results to handle large datasets efficiently.

Test actions on a development page or test user before using live accounts.