Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user posts through facebook-sdk python api?

I use facebook-jssdk to authorize my application for read access to user profile and user posts.

FB.login(
    function(response) { },  
    {scope:'user_status,user_likes,user_photos,user_videos,user_questions,read_stream,user_posts'}
);

Then I'm trying to receive user posts. import json from time import mktime from urllib import urlopen from facebook import GraphAPI from datetime import datetime

ga = GraphAPI()
access_token = ga.get_app_access_token(settings.FACEBOOK_APP_ID,     settings.FACEBOOK_APP_SECRET)

url = "https://graph.facebook.com/fql?q=SELECT+created_time+FROM+stream+ WHERE+source_id=%s+AND+created_time<%d&access_token=%s" % (
social_id,   # user id in facebook, taken from facebook-jssdk
int(mktime((datetime.now().timetuple()))),
access_token
)
data = json.loads(urlopen(url).read())
logger.debug(data)
# {u'data': []}


ga = GraphAPI(access_token)
ga.get_object('/508708815952422/posts/', token=token)
# {u'data': []}

Data is always empty... What am I doing wrong?

Usefull info: Since "Facebook Login v2.5" permissions require review to work correctly. https://developers.facebook.com/docs/facebook-login/permissions/v2.2

like image 402
Yevgeniy Shchemelev Avatar asked Sep 29 '15 07:09

Yevgeniy Shchemelev


1 Answers

You want to know how to get user posts using a python api, right?

I'm using facebook-sdk within a django project and I got it to work, like this (Implementation - services/facebook.py):

from django.conf import settings
import facebook
import requests

class FacebookFeed:
    token_url = 'https://graph.facebook.com/oauth/access_token'
    params = dict(client_id=settings.SOCIAL_AUTH_FACEBOOK_KEY, client_secret=settings.SOCIAL_AUTH_FACEBOOK_SECRET,
                  grant_type='client_credentials')

    @classmethod
    def get_posts(cls, user, count=6):
        try:
            token_response = requests.get(url=cls.token_url, params=cls.params)
            access_token = token_response.text.split('=')[1]
            graph = facebook.GraphAPI(access_token)
            profile = graph.get_object(user)
            query_string = 'posts?limit={0}'.format(count)
            posts = graph.get_connections(profile['id'], query_string)
            return posts
        except facebook.GraphAPIError:
            return None

Note: In my case I need to fetch the access token using the client-credentials flow, making use of the Key and Secret settings, if you're logging users into an app of yours and already have tokens on your side, then ignore the lines:

token_response = requests.get(url=cls.token_url, params=cls.params)
access_token = token_response.text.split('=')[1]

Usage (views.py):

from django.http import HttpResponse
from app.services.social_networks.facebook import FacebookFeed

def get_facebook_posts(request, user):
    posts = FacebookFeed.get_posts(user=user)
    if not posts:
        return HttpResponse(status=500, content="Can't fetch posts for desired user", content_type="application/json")
    return HttpResponse(json.dumps(posts), content_type="application/json")

Hope this helps, any problem, please do ask =)

like image 83
jlnabais Avatar answered Oct 23 '22 22:10

jlnabais