Search code examples
pythonfacebookfacebook-pagefacebook-sdk-4.0

What permission(s) do I need to read the names of the users who posted to a Facebook page?


I want to write a Python script that reads the names of the Facebook users who wrote a message on my Facebook Page.

I'm using the facebook-sdk Python library. The lines

graph = facebook.GraphAPI(page_access_token)
profile = graph.get_object(profile_id)
posts = graph.get_connections(profile['id'], 'feed')
print(posts['data'][0]['message'])
print(posts['data'][0]['from']['name'])

work fine when I use a short-lived access token retrieved by just copying it from the Graph API explorer.

But when I generate a never-expiring page access token (read here), there is an error in the last line where the Python script wants to retrieve the name. (KeyError: 'from')

So how do i set the right permissions? The access token has user_about_me, user_posts, read_page_mailboxes, manage_pages, publish_pages, public_profile permissions, but it still doesn't work. Maybe I did something wrong setting up the app I created to retrieve a new access token?

Or maybe this isn't a permissions issue after all? (See my previous question.)


Solution

  • You need to specify the fields you want to retrieve manually if you're using v2.4 or higher. Unfortunately this is not documented in the project you're using:

    posts = graph.get_connections(profile['id'], 'feed', 'fields=id,message,from')
    

    should work I guess according to https://github.com/pythonforfacebook/facebook-sdk/blob/master/facebook/init.py#L117