Prerequisites
Before you begin, ensure you have:
- A Facebook account with admin access to the Page(s) you want to post to
- Basic understanding of HTTP requests and APIs
- A development environment (code editor, terminal, etc.)
- Node.js or similar environment for making API requests
Setting Up a Facebook App
Step 1: Create a Facebook Developer Account
- Go to Facebook for Developers
- Click "Get Started" and follow the prompts to create a developer account
- Accept the developer terms and conditions
Step 2: Create a New App
- In the Facebook Developer Dashboard, click "Create App"
- Select "None" as the app type (for custom integrations)
- Enter your app name and contact email
- Click "Create App ID"
Step 3: Configure Basic Settings
- Go to "Settings" > "Basic" in your app dashboard
- Fill in basic information:
- Display Name
- Contact Email
- App Domains (if applicable)
- Privacy Policy URL
- Terms of Service URL
- Click "Save Changes"
Getting Required Permissions
Step 4: Add Facebook Login Product
- In your app dashboard, go to "Products" > "Add Product"
- Click "Set Up" for Facebook Login
- Select "Web" as the platform
- Enter your site URL (can be localhost for development)
- Click "Save"
Step 5: Request Required Permissions
For posting to Pages, you'll need:
pages_manage_posts - Manage posts on Pages
pages_read_engagement - Read Page content
publish_pages - Publish content to Pages
Add these to your app's permissions in the Facebook Login settings.
Generating Access Tokens
Step 6: Get User Access Token
Use the Facebook Login dialog with the required permissions:
// Example using Facebook JavaScript SDK
FB.login(function(response) {
if (response.authResponse) {
console.log('Access Token:', response.authResponse.accessToken);
}
}, {scope: 'pages_manage_posts,pages_read_engagement,publish_pages'});
Step 7: Get Page Access Token
- First, get the user's Pages:
GET https://graph.facebook.com/v12.0/me/accounts?access_token={user-access-token}
- From the response, find your Page and use its access token
Step 8: Get Long-Lived Access Token
Exchange short-lived tokens for long-lived ones:
GET https://graph.facebook.com/v12.0/oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={short-lived-token}
Making API Requests
Step 9: Basic API Request Structure
Facebook API requests follow this pattern:
POST https://graph.facebook.com/v12.0/{page-id}/feed
?message={your-message}
&access_token={page-access-token}
Step 10: Using Different HTTP Methods
GET - Retrieve data
POST - Create new content
DELETE - Remove content
PUT - Update existing content
Posting to Pages
Step 11: Simple Text Post
POST https://graph.facebook.com/v12.0/{page-id}/feed
?message=Hello%20World!
&access_token={page-access-token}
Step 12: Post with Link
POST https://graph.facebook.com/v12.0/{page-id}/feed
?message=Check%20out%20this%20link
&link=https://example.com
&access_token={page-access-token}
Step 13: Scheduled Post
POST https://graph.facebook.com/v12.0/{page-id}/feed
?message=Scheduled%20post
&published=false
&scheduled_publish_time=1633046000
&access_token={page-access-token}
Handling Different Post Types
Step 14: Photo Posts
POST https://graph.facebook.com/v12.0/{page-id}/photos
?url={image-url}
&caption=Photo%20caption
&access_token={page-access-token}
Step 15: Video Posts
POST https://graph.facebook.com/v12.0/{page-id}/videos
?source={video-url}
&description=Video%20description
&access_token={page-access-token}
Step 16: Album Posts
// First create album
POST https://graph.facebook.com/v12.0/{page-id}/albums
?name=Album%20Name
&access_token={page-access-token}
// Then add photos to album
POST https://graph.facebook.com/v12.0/{album-id}/photos
?url={image-url}
&access_token={page-access-token}
Error Handling and Best Practices
Step 17: Error Handling
// Example error handling
try {
const response = await fetch(facebookApiUrl, {
method: 'POST',
body: formData
});
if (!response.ok) {
const error = await response.json();
console.error('Facebook API Error:', error);
// Handle specific error codes
}
} catch (error) {
console.error('Network error:', error);
}
Step 18: Best Practices
- Always use HTTPS for API requests
- Store access tokens securely
- Implement token refresh logic
- Handle rate limits gracefully
- Validate all user input
- Use proper error handling
Rate Limits and API Usage
Step 19: Understanding Rate Limits
- Facebook API has strict rate limits
- Limits vary by endpoint and app type
- Check headers for rate limit information:
X-Business-Use-Case-Usage
X-Ad-Account-Usage
Step 20: Monitoring API Usage
GET https://graph.facebook.com/v12.0/{app-id}/application
?fields=restrictions
&access_token={app-access-token}
Troubleshooting
Common Issues and Solutions
- Permission Errors: Ensure you have all required permissions
- Token Expiration: Use long-lived tokens and implement refresh logic
- Rate Limits: Implement exponential backoff for retries
- API Versioning: Always specify API version in URLs
- App Review: Some permissions require Facebook review
Debugging Tools
- Facebook Graph API Explorer
- Facebook Developer Debugger
- Browser developer tools for network inspection
Note: Always refer to the official Facebook documentation for the most up-to-date information and API changes.
Security Warning: Never expose access tokens in client-side code. Use server-side authentication and implement proper token storage.