简体   繁体   English

Facebook graphAPI使用python获取朋友帖子的ID

[英]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. 我正在使用python facebook-sdk(修改后的示例代码)从Facebook墙获取供稿。 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. 我的脚本运行良好,直到有2个以上朋友的帖子为止-facebook开始将这些帖子分组,而我不再能够获得该帖子的ID-仅是第一个。

This is what I mean by "grouped posts": fb grouped wall posts 这就是“分组帖子”的意思: fb分组墙帖子

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. 现在,如果不超过2个朋友的帖子并且未将其分组,则打印结果会给我成功的结果。 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. 一切顺利,我可以将帖子ID用于get_it方法。

However, once there are more then 2 friend's posts, it gets grouped and suddenly a new thing 'story' appears in the dict: 但是,一旦有多于2个朋友的帖子,它就会被分组,然后突然在字典中出现一个新东西“故事”:

'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. “故事” ID仅适用于分组留言墙中的最后评论。

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. 此外,由于某种原因下一个dict字段不会在get_it()方法中传递,并且print(post ['message'])和print(post)返回空白结果。

How to solve this, so I get IDs and messages of all friends' posts on my wall as well as mine, please? 如何解决此问题,所以我在我的墙上以及我的墙上都会收到所有朋友的帖子的ID和消息? I am really lost, tried everything. 我真的迷路了,尝试了一切。 Thank you. 谢谢。

This was an intentional update by facebook to stop automating scripts and spamming. 这是Facebook的有意更新,目的是停止脚本和垃圾邮件的自动化。

It will not be rolled back unfortunately. 不幸的是,它不会回滚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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