This client library is designed to support the Facebook Graph API, providing a Python interface to interact with Facebook's social graph objects like users, posts, pages, and events. It also supports the official Facebook JavaScript SDK for authentication. The current stable version is 3.1.0, released in November 2018. While this project is maintained, users dealing with Meta Business (Marketing) APIs should consider the 'facebook-python-business-sdk' which is more actively developed for those specific use cases.
pip install facebook-sdkVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the `GraphAPI` client and fetch basic user profile information. You need an access token, which can be obtained from the Facebook Developer Tools. Remember to grant appropriate permissions for the data you wish to access. Always specify the Graph API version.
Upgrade Python to 3.5+ (Python 2.7 support is removed in v4.0.0-pre). Update Graph API calls to supported versions. Replace `put_wall_post` with `put_object`.
Initialize `GraphAPI` with `version='x.y'` (e.g., `GraphAPI(ACCESS_TOKEN, version='2.12')`). Regularly check Meta's Graph API changelog for updates.
Ensure your access token is valid and unexpired. Obtain the correct permissions for the data you are trying to access. Handle `GraphAPIError` exceptions for authentication and permission issues gracefully. Use long-lived access tokens where appropriate.
If your application targets business-related functionalities, migrate to `facebook-python-business-sdk` and its `facebook_business` module. Note that this is a separate library with different import paths and usage patterns.
Ensure your environment uses Python 3.5+ (preferably 3.8+ for 4.0.0-pre) and update your Graph API version parameter to a supported version for the 4.x series.
import facebook
Obtain a valid, non-expired Facebook access token (e.g., user, page, or app token) and pass it correctly when initializing the `GraphAPI` object.
Publishing directly to a user's feed using `publish_actions` is no longer possible; consider using Facebook's Share Dialog or Open Graph stories for user-initiated shares, or ensure you have appropriate page permissions (e.g., `pages_manage_posts`) with a page access token for page posts.
Ensure your app has requested and been granted the necessary permissions (e.g., `pages_read_user_content`) and submit your app for review by Facebook if required for public data access.