Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all content posted by a Facebook Group using Graph API

I am very new to the Graph API and trying to write a simple python script that first identifies all pages that a user has liked and all groups that he/she is a part of. To do this, I used the following:

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)

Now that I can see the id's of the groups in the JSON output, I want to hit them one by one and fetch all content (posts, comments, photos etc.) posted within that group. Is this possible and if yes, how can I do it? What API calls do I have to make?

like image 879
Darth Coder Avatar asked Apr 04 '14 16:04

Darth Coder


3 Answers

That's quite a broad question, before asking here you should have give a try searching on SO.

Anyways, I'll tell you broadly how can you do it.

First of all go through the official documentation of Graph API: Graph API Reference.
You'll find each and every API which can be used to fetch the data. For example: /group, /page. You'll get to know what kind of access token with what permissions are required for an API call.

Here are some API calls useful to you-

  • to fetch the group/page's posts- /{group-id/page-id}/posts

  • to fetch the comments of a post- {post-id}/comments

  • to fetch the group/page's photos- /{group-id/page-id}/photos

and so on. Once you'll go through the documentation and test some API calls, the things would be much clear. It's quite easy!

Hope it helps. Good luck!

like image 171
Sahil Mittal Avatar answered Oct 19 '22 20:10

Sahil Mittal


Here's an example using 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...
like image 23
aldorath Avatar answered Oct 19 '22 20:10

aldorath


Check the API reference. You should use feed.

You can use /{group-id}/feed to get an array of Post objects of the group. Remember to include a user access token for a member of the group.

like image 2
Amaury Medeiros Avatar answered Oct 19 '22 18:10

Amaury Medeiros