Registry / communication / facebook-sdk

facebook-sdk

JSON →
library3.1.0pypypi✓ verified 86d ago

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-sdk
INSTALL
IMPORT
SIG · FACEBOOK-SDK
F
facebook-sdk
communicationpythonv3.1.0
Install
2.1s avg
Import
592ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.625s · 21.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.1s · import 0.559s · 22MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GraphAPI
import facebook graph = facebook.GraphAPI(...)
The primary class for interacting with the Facebook Graph API.

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.

import os import facebook # It is highly recommended to use environment variables for sensitive data like tokens ACCESS_TOKEN = os.environ.get('FB_ACCESS_TOKEN', 'YOUR_ACCESS_TOKEN_HERE') if not ACCESS_TOKEN or ACCESS_TOKEN == 'YOUR_ACCESS_TOKEN_HERE': print("Please set the FB_ACCESS_TOKEN environment variable or replace 'YOUR_ACCESS_TOKEN_HERE' with your actual access token.") else: try: # Initialize the Graph API with your access token # Specify the API version for forward compatibility graph = facebook.GraphAPI(ACCESS_TOKEN, version="2.12") # Use a specific version, e.g., "2.12" or higher # Get information about the current user ('me') profile = graph.get_object('me', fields='id,name,email') print(f"Hello, {profile['name']}! Your ID is {profile['id']} and email is {profile.get('email', 'not provided')}.") # Example: Get a list of friends (requires 'user_friends' permission) # Note: This will only return friends who have also used the app. # friends = graph.get_connections('me', 'friends') # print("Your friends who use this app:") # for friend in friends['data']: # print(f"- {friend['name']} (ID: {friend['id']})") except facebook.GraphAPIError as e: print(f"Facebook Graph API Error: {e.type} - {e.message}") except Exception as e: print(f"An unexpected error occurred: {e}")
Debug
Known issues
breakingVersion 3.0.0 introduced significant breaking changes. It removed support for Python 2.6 and 3.3, and older Graph API versions (2.1-2.6). The `put_wall_post` method was removed; use `put_object` instead.
fix
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`.
affects: >=3.0.0
gotchaFacebook's Graph API undergoes frequent version changes and deprecations. Always specify the `version` parameter when initializing `facebook.GraphAPI` to avoid unexpected behavior or API calls defaulting to an unsupported or deprecated version. For instance, `facebook-sdk` v3.1.0 defaulted to Graph API v2.7, which is now very old.
fix
Initialize `GraphAPI` with `version='x.y'` (e.g., `GraphAPI(ACCESS_TOKEN, version='2.12')`). Regularly check Meta's Graph API changelog for updates.
affects: <4.0.0
gotchaAccess tokens are central to authentication and have varying expiration times and required permissions. Using expired tokens or lacking necessary permissions will result in `GraphAPIError` exceptions. User `user_friends` permission will only return friends who have also authorized your app.
fix
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.
affects: All
deprecatedFor applications interacting with Meta's Business (e.g., Marketing, Ads, Pages, Business Manager, Instagram) APIs, the official and actively developed SDK is `facebook-python-business-sdk` (PyPI package `facebook_business`). The `facebook-sdk` library is primarily for general Graph API access and may not include the latest features or optimal support for business-specific functionalities.
fix
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.
affects: All
breakingThe upcoming version 4.0.0 (currently in pre-release) explicitly removes support for Python 2.7 and 3.4. It also removes support for Graph API versions 2.8, 2.9, 2.10, 2.11, 2.12, and 3.0, updating default Graph API version to 2.10.
fix
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.
affects: >=4.0.0-pre
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'facebook_sdk'
The Python package `facebook-sdk` is installed, but the correct module name for importing is `facebook`, not `facebook_sdk`.
fix
import facebook
GraphAPIError: An active access token must be used to query information about the current user.
The provided Facebook access token is missing, expired, revoked, or invalid, preventing successful authentication with the Graph API.
fix
Obtain a valid, non-expired Facebook access token (e.g., user, page, or app token) and pass it correctly when initializing the `GraphAPI` object.
GraphAPIError: (#200) Requires extended permission: publish_actions
The `publish_actions` permission, which was historically used to post to a user's feed, was deprecated by Facebook and is no longer supported by the Graph API.
fix
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.
GraphAPIError: (#10) To use 'Page Public Content Access', your use of this endpoint must be reviewed and approved by Facebook.
Accessing certain public content or performing specific actions on behalf of a user or page requires your Facebook app to undergo and pass Facebook's app review process for the relevant permissions.
fix
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.
Upgrade
Version history
3.1.0latest on PyPI · released Nov 6, 2018
Audit
Dependencies
requestsrequiredRequired for making HTTP requests to the Graph API.
Agent activity
38 hits · last 30 days
node
34
OpenAI (training)
1
Resources
facebook-sdk — pip install facebook-sdk · libregistry