如何使用Graph API获取Facebook群组发布的所有内容

2024-04-27 03:10:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Graph API非常陌生,我试图编写一个简单的python脚本,该脚本首先标识用户喜欢的所有页面和他/她所属的所有组。为此,我使用了以下命令:

To get the groups he has joined:

API: /{user-id}/groups 
Permissions req: user_groups 


To get the pages he has liked:

API: /{user-id}/likes 
Permissions req: user_likes

and 
    url='https://graph.facebook.com/'+userId+'/likes?access_token='+accessToken +'&limit='+str(limit)

现在我可以在JSON输出中看到组的id,我想逐个点击它们并获取该组中发布的所有内容(帖子、评论、照片等)。这有可能吗?如果有,我怎么办?我需要进行哪些API调用?


Tags: theto脚本apiidpermissionsgetreq
3条回答

这是一个很宽泛的问题,在问这个问题之前,你应该试着搜索一下。

不管怎样,我会广泛地告诉你怎么做。

首先查看Graph API的官方文档:Graph API Reference
您将找到每个可用于获取数据的API。例如:^{}^{}。您将了解API调用需要什么样的access token权限。

以下是一些对您有用的API调用-

  • 获取组/页的帖子-/{group-id/page-id}/posts

  • 获取帖子的评论-{post-id}/comments

  • 获取组/页的照片-/{group-id/page-id}/photos

等等。一旦您阅读了文档并测试了一些API调用,事情就会很清楚了。很简单!

希望有帮助。祝你好运!

下面是一个使用facepy的示例:

from facepy import GraphAPI
import json

graph = GraphAPI(APP_TOKEN)

groupIDs = ("[id here]","[etc]")

outfile_name ="teacher-groups-summary-export-data.csv"
f = csv.writer(open(outfile_name, "wb+"))

for gID in groupIDs:
  groupData = graph.get(gID + "/feed", page=True, retry=3, limit=500)


    for data in groupData:
        json_data=json.dumps(data, indent = 4,cls=DecimalEncoder)
        decoded_response = json_data.decode("UTF-8")
        data = json.loads(decoded_response)
        print "Paging group data..."

        for item in data["data"]:
            ...etc, dealing with items...

检查API引用。你应该用feed

可以使用/{group-id}/feed获取组的Post objects数组。记住要包含组成员的用户访问令牌。

相关问题 更多 >