Implementing Facebook FQL in Python

It will probably be obvious soon enough, but I am new to coding and StackOverflow -- apologies if I come off as a bit oblivious.

I have been trying to query Facebook with FQL out of a function on my views.py page in a Django app. I did have a working solution utilizing the Facebook Graph API, but it was ultimately a poor solution since I am aiming to retrieve the user’s newsfeed.

While there are a number of answers relating to Facebook FQL in PHP, there are no explicit examples in Python. Instead, most of the questions are just related to the syntax of the query, and not how to actually integrate it into an actual function.

The query I am looking to make is:

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’)

As I understand, the best way to make this request is using Requests but frankly, I am just looking for a working solution.

I considered posting some of my non-working code up here, but it has pretty much spiraled into a mess.

Any help or direction is greatly appreciated!

UPDATE Just wanted to document where I am at, with Sohan Jain's help, I've gotten to this point:

def get_fb_post(request):
    user_id = UserSocialAuth.objects.get(user=request.user)
    api = facebook.GraphAPI(user_id.tokens)
    # 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')''')        
    print(response)

    data = {'profile': '...something...' } # what should be going in here?
    return HttpResponse(json.dumps(data), mimetype='application/json')

In PyCharm I am getting the following (link to image if you prefer: http://i.stack.imgur.com/8sZDF.png):

/Library/Python/2.7/site-packages/facebook.py:52: DeprecationWarning: django.utils.simplejson is deprecated; use json instead.

from django.utils import simplejson as json

In my browser I am getting this(image link again: http://i.stack.imgur.com/njXo8.png):

GraphAPIError at /fbpost/

I am not sure how to fix the DeprecationWarning. I tried at the top of the file putting "from django.utils import simplejson as json" but that didn't work (I thought it was probably a bit too simplistic, but worth a shot).

If I replace the query with this:

response = api.fql("SELECT name, type, value, filter_key FROM stream_filter WHERE uid = me()")

I do get a very long response that looks like this:

[{u'filter_key': u'nf', u'type': u'newsfeed', u'name': u'News Feed', u'value': None}, {u'filter_key'... ...}]

It goes on and on and does appear to be returning valid information.

Answers


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.


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.


Need Your Help

Stopping jQuery equal height divs from calculating hidden content

javascript jquery

I am using the following jQuery to giv certain divs with the id's #main, #sidebar, #side-event equal height. On most pages, I am using .hide(); to toggle information within the content section of the

Installation of openoffice 4.0 for open erp

openerp openoffice.org

i download open office 4.0 and i provide username = amber last name= xyz