[Answer]-Implementing Facebook FQL in Python

1👍

I have figured out how to get a response, the code looks something like this:

from facepy import GraphAPI

def get_fb_post(request):
user_id = UserSocialAuth.objects.get(user=request.user)
access_token = user_id.tokens
graph = GraphAPI(access_token)
data = graph.fql('''SELECT post_id, app_id, source_id, updated_time, filter_key, attribution, message,
          action_links, likes, permalink FROM stream WHERE filter_key IN (SELECT filter_key FROM stream_filter
          WHERE uid = me() AND type = 'newsfeed')''')
return HttpResponse(json.dumps(data), mimetype='application/json')

This is returning the information that I was looking for, which looks something like this:

{u’data’: [{u’filter_key’: u’nf’, u’permalink’: u’facebook-link’, u’attribution’: None, u’app_id’: u’123456789′, u’updated_time’: 1398182407, u’post_id’: u’1482173147_104536679665087666′, u’likes’: …etc, etc, etc….

Now, I am just going to try and randomize the displayed post by randomly iterating through the JSON — granted, this is outside of the scope of my question here. Thanks to those who helped.

0👍

You should check out the python facebook-sdk library*. Here’s an example usage to run your query. facebook-sdk is ultimately a wrapper around the requests library, but adds a lot of convenience.

import facebook
api = facebook.GraphAPI(client_access_token)
# response gets deserialized into a python object
response = api.fql('''
    SELECT post_id, app_id, source_id, updated_time, filter_key, attribution, message, action_links, likes, permalink FROM stream WHERE filter_key IN (SELECT filter_key FROM stream_filter WHERE uid = me() AND type = 'newsfeed’)
''')

*N.B. as of writing, there’s a small bug with the fql method, as documented here. You’ll probably want to use a fork of that repo that fixes the bug, like the one in the pull request.

Leave a comment