使用 Facebook Graph API 获取我所有的 public 帖子

Getting all my public posts using Facebook Graph API

如何使用 python 代码和 facebook 图 api 获取我所有的 facebook 帖子。 我试过使用这段代码:

import json
import facebook


def get_basic_info(token):

    graph = facebook.GraphAPI(token)

    profile = graph.get_object('me',fields='first_name,last_name,location,link,email')  

    print(json.dumps(profile, indent=5))

def get_all_posts(token):
    graph = facebook.GraphAPI(token)
    events = graph.request('type=event&limit=10000')
    print(events)


def main():

    token = "my_token"
    #get_basic_info(token)
    get_all_posts(token)



if __name__ == '__main__':

    main()

我收到一条错误消息说, "GraphAPIError: (#33) This object does not exist or does not support this action".

似乎所有其他 Whosebug 问题都很老,不适用于最新版本的 facebook graph API。我不完全确定你是否可以使用 facebook graph api 来做到这一点。 如果使用此技术无法做到这一点,是否有任何其他方法可以使用 python 获取我的帖子? 请注意函数 get_basic_info() 运行良好。

我假设您想获取用户事件:https://developers.facebook.com/docs/graph-api/reference/user/events/

注意:

This edge is only available to a limited number of approved apps. Unapproved apps querying this edge will receive an empty data set in response. You cannot request access to this edge at this time.

无论哪种方式,API 都不会是 type=event&limit=10000,而是 /me/events

我已经在@luschn 的第一个答案的帮助下解决了这个问题 我又犯了一个错误,那就是使用事件来获取我应该在我的代码中使用 me/posts 的所有 posts.instead。 这是在版本 6 中完美运行的功能。

def get_all_posts(graph):

    posts = graph.request('/me/posts')
    count=1
    while "paging" in posts: 
        print("length of the dictionary",len(posts))
        print("length of the data part",len(posts['data']))
        for post in posts["data"]:
            print(count,"\n")
            if "message" in post:   #because some posts may not have a caption
                print(post["message"]) 
            print("time :  ",post["created_time"])
            print("id   :",post["id"],"\n\n")
            count=count+1

        posts=requests.get(posts["paging"]["next"]).json()

    print("end of posts")

这里post["data"]只给出了前25个帖子,所以我用posts["paging"]["next"]link只要有下一页就可以得到下一页[=14] =]