相关文章推荐
乐观的火锅  ·  matlab ...·  7 月前    · 
风流的火柴  ·  js数组对象求和-掘金·  1 年前    · 

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

0 人关注

我怎样才能使用python代码和facebook graph 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".

似乎所有其他的stackoverflow问题都很老了,不适用于最新版本的facebook graph API。我不完全确定你是否可以用facebook graph api来做这个。 如果使用这种技术不可能,有没有其他方法可以让我用python获得我的帖子? 请注意,函数get_basic_info()工作得很好。

4 个评论
那么问题出在哪里呢?你调试了代码吗?确切的api响应是什么?
我得到一个错误,说:"GraphAPIError: (#33) This object does not exist or does not support this action"
你是否使用了一个用户令牌,它是否包括所有必要的权限?
是的,它是一个用户令牌,它有所有的权限。
python
facebook
facebook-graph-api
Mahmud Mushfique
Mahmud Mushfique
发布于 2020-02-18
2 个回答
andyrandy
andyrandy
发布于 2020-02-19
已采纳
0 人赞同

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

这个边缘只对数量有限的批准的应用程序可用。未经批准的应用程序查询此边缘将收到一个空的数据集作为回应。你现在不能请求访问这个边缘。

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

Mahmud Mushfique
Mahmud Mushfique
发布于 2020-02-19
0 人赞同

在@luschn的第一个答案的帮助下,我已经解决了这个问题。 我还犯了一个错误,那就是使用事件来获取我所有的帖子,而我应该在代码中使用 me/posts 。 以下是在第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()