Ask Your Question

How to retrieve the user who has commented on a page post using Graph API?

asked 2022-08-22 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-02-09 18:00:00 +0000

djk gravatar image

To retrieve the user who has commented on a page post using Graph API, you can use the following steps:

  1. Get the post ID: You'll need to know the ID of the post for which you want to retrieve the comments. You can get the post ID by using the /{page-id}/feed endpoint or by visiting the page and finding the post ID in the URL.

  2. Get the comments: Once you have the post ID, you can use the /{post-id}/comments endpoint to retrieve the comments for that post. This will return an array of comment objects.

  3. Retrieve the user data: Each comment object will contain a "from" field which includes information about the user who posted the comment, including their name and profile picture. You can use this information to retrieve additional user data, such as their ID or email, by using the /{user-id} endpoint or the /search endpoint.

Here's an example of how you can retrieve the user who has commented on a page post using Graph API in Python:

import facebook

# Set up the Graph API client
graph = facebook.GraphAPI(access_token='YOUR_ACCESS_TOKEN', version='3.0')

# Get the post ID
post_id = 'POST_ID'

# Get the comments for the post
comments = graph.get_connections(id=post_id, connection_name='comments')

# Loop through the comments and retrieve the user data for each comment
for comment in comments['data']:
    user_data = graph.get_object(id=comment['from']['id'], fields='id,name,email')

This script will print out the ID, name, and email of each user who has commented on the post. Note that you'll need to replace 'YOURACCESSTOKEN' with a valid access token for your app, and 'POST_ID' with the ID of the post you want to retrieve the comments for.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2022-08-22 11:00:00 +0000

Seen: 9 times

Last updated: Feb 09 '22