简体   繁体   中英

Facebook graphAPI get id of friend posts with python

I am using python facebook-sdk (modified example code) to get feed from my facebook wall. That means my posts and my friends' posts. My scripts works just fine until there is more then 2 friend's posts - facebook starts grouping those posts and I can no longer get that post's ID - only of the first one.

This is what I mean by "grouped posts": fb grouped wall posts

Here is my code:

import facebook
import requests


def get_it(post):

    print(post['message'])
    print(post)

access_token = '<mytoken>'
user = 'me'
graph = facebook.GraphAPI(access_token)

posts = graph.get_connections(user, 'feed', limit=20)
print(posts) 

while True:
    try:
        [get_it(post=post) for post in posts['data']]

        posts = requests.get(posts['paging']['next']).json()
    except KeyError:
        break

Now the prints give me successful results if there isn't more than 2 friend's posts and they aren't grouped. the dict looks like this print(posts):

 {'paging': {'previous': 'https://graph.facebook.com/v2.5/XXX&access_token=XXX&__paging_token=XXX&__previous=1', 'next': 'https://graph.facebook.com/v2.5/XXX/feed?limit=20&access_token=XXX&__paging_token=XXX'}, 'data': [{'id': 'XXXXXX_XXXX', 'created_time': '2016-03-31T23:58:41+0000', 'message': 'test'}, {'id': 'XXX_XXX', 'created_time': '2016-03-31T23:33:43+0000', 'message': 'hehe'}]}

And everything goes fine, I can use the post IDs for the get_it method.

However, once there are more then 2 friend's posts, it gets grouped and suddenly a new thing 'story' appears in the dict:

'data': [{'created_time': '2016-04-01T02:32:37+0000', 'story': 'XXX wrote on your Timeline.', 'id': '123_456'}, {'created_time': '2016-03-31T23:58:41+0000', 'message': 'test', 'id': '6666_6666'}

The 'story' ID works only for the LAST comment made in grouped wall posts.

Additionally, the next dict fields for some reason don't get passed in the get_it() method and print(post['message']) and print(post) return blank results.

How to solve this, so I get IDs and messages of all friends' posts on my wall as well as mine, please? I am really lost, tried everything. Thank you.

This was an intentional update by facebook to stop automating scripts and spamming.

It will not be rolled back unfortunately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM