Get Facebook Post Comments Using API
import logging
from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel
import os
from facebook_comments_fetcher import fetch_facebook_post_comments
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)
app = FastAPI()
class PostID(BaseModel):
post_id: str
@app.post("/fetch-comments/")
def fetch_comments(post_data: PostID):
access_token = os.environ.get("FACEBOOK_ACCESS_TOKEN")
if not access_token:
raise HTTPException(status_code=500, detail="Facebook access token is not configured.")
try:
comments = fetch_facebook_post_comments(post_data.post_id, access_token)
return comments
except HTTPException as e:
raise HTTPException(status_code=e.status_code, detail=e.detail)
Created: | Last Updated:
Introduction to the Fetch Facebook Post Comments Template
Welcome to the Lazy template guide for fetching comments from a Facebook post using the Facebook API. This template is designed to help you quickly set up an application that can retrieve comments from a specific Facebook post. It's perfect for social media managers, marketers, or developers who need to analyze engagement on their Facebook pages.
Getting Started
To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.
Initial Setup: Adding Environment Secrets
Before you can fetch comments from Facebook, you'll need to set up an environment secret for your Facebook access token. This token is required to authenticate your requests to the Facebook API.
- Go to the Facebook Developer Portal and create an app if you haven't already.
- Under your app's settings, navigate to the "Permissions and Features" section.
- Request and obtain the following permissions:
pages_read_engagement
,pages_manage_metadata
, andpages_read_user_content
. - Once you have the permissions, generate an access token for your app.
- In the Lazy Builder interface, go to the Environment Secrets tab.
- Create a new secret with the key
FACEBOOK_ACCESS_TOKEN
and paste your generated access token as the value.
Test: Pressing the Test Button
With your environment secret set, you're ready to test the application. Press the "Test" button on the Lazy platform. This will deploy your app and launch the Lazy CLI.
Entering Input: Filling in User Input
After pressing the "Test" button, if the application requires any user input, the Lazy CLI will prompt you to provide it. For this template, you will need to enter the Facebook post ID for which you want to fetch comments.
Using the App
Once the app is running, Lazy will provide you with a dedicated server link to use the API. Additionally, since this app uses FastAPI, you will also receive a link to the API documentation. This documentation will guide you on how to make requests to your new endpoint and fetch comments from a Facebook post.
Integrating the App
After successfully fetching comments, you may want to integrate this functionality into your existing systems or frontend applications. Here's how you can do that:
- Use the server link provided by Lazy to make HTTP POST requests to the
/fetch-comments/
endpoint from your frontend or service. - Include the Facebook post ID in the body of your request as JSON.
- Handle the response from the API, which will include the comments data, in your application.
Here's a sample request you might make from another application:
POST /fetch-comments/ HTTP/1.1<br>
Host: [Your Lazy Server Link]<br>
Content-Type: application/json<br>
<br>
{<br>
"post_id": "1234567890123456"<br>
}
And a sample response you might receive:
{<br>
"data": [<br>
{<br>
"from": {<br>
"name": "User Name",<br>
"id": "1234567890"<br>
},<br>
"message": "This is a comment!",<br>
"id": "0987654321"<br>
},<br>
...<br>
],<br>
"paging": { ... },<br>
"summary": { ... }<br>
}
By following these steps, you can seamlessly integrate the Fetch Facebook Post Comments app into your workflow, allowing you to gather valuable insights from your Facebook audience.