Estimated reading time: 2 minutes, 56 seconds

How to make a Facebook feed using the Graph API, JSON and jQuery

Hey all,
After scouring the internet for a tutorial I took it on myself (after realising there was none) to teach the world how to use the new cool Facebook graph api with JSON along side with jQuery to make a cool feed for use on websites.

From this you can do anything and even do a live feed using ajax. Maybe i’ll do a tutorial on that sometime soon.

UPDATE: Facebook have recently changed their api and policy. To access a feed now requires an access token. We will create a new tutorial soon. P.S. Please don’t copy the code in the source on our working feed, this will expire and is insecure to use on your own website, thanks.

Follow @PrettyKlicks for more news and tips.

CRITICAL NOTICE: There was a security vulnerability kindly pointed out by Rick, this can be fixed very easily. Please update your code. See below!

Check out our demo here: DEMO

So first things first. You need to set up your page for jQuery by adding the lines:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
You need to place this into the tags.

The URL

Now we need to identify where our json results are coming from facebook and the url for it so we can fetch the data.

This is our url for Pretty Klicks:

http://graph.facebook.com/prettyklicks/feed

Notice the /feed this is because we need to tell facebook not only is it the username prettyklicks but also we want to get the results for the feed. We’re not done yet. We need to add some extra things to the url.

http://graph.facebook.com/prettyklicks/feed?limit=5&callback=?

First of all we called the limit parameter to tell facebook we only want the 5 latest wall posts and more importantly we added callback=? This is because JSON is not meant to work cross domain. Therefore so we can work around this and to help jQuery set the callback itself and then process our JSONP we add the ‘?’ to let jQuery do it for us.

Parsing the JSON result with our jQuery function

Right so now we need to create our javascript to parse the json results. I have commented the code to show you what each bit does.

function fbFetch(){
//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
var url = "http://graph.facebook.com/prettyklicks/feed?limit=5&callback=?";

//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
$.getJSON(url,function(json){
var html = "<ul>";

//loop through and within data array's retrieve the message variable.
//XSS ATTACK FIX AS OF 6/1/12 PLEASE UPDATE TO THIS ASAP.
$.each(json.data,function(i,fb){
html += "<li>" + $('<div>').html(fb.message).text() + "</li>";
});

html += "</ul>";

//A little animation once fetched
$('.facebookfeed').animate({opacity:0}, 500, function(){
$('.facebookfeed').html(html);
});
$('.facebookfeed').animate({opacity:1}, 500);
});
};

(I just broke it into 3 sections to make it easier to read)

Now we have our function set in thewe need to set out our html and initiate the function onload. Our html should look something like this at the very least.

<body onLoad="fbFetch()">
<div class="facebookfeed">Loading...</div>

And thats a wrap! All we need to do now is style it out.

Check out our demo here: DEMO

And lastly please feel free to comment. Feel free to use this script. All we ask is you follow us on twitter @PrettyKlicks and maybe give us a some credit for it via a link back :)

….Some shameless seo:
Deeside Web Design

Posted in Tips & Tricks | Tags: , , , | 63 Comments
By Stefan |

60 Comments

  • Hi,

    I used this on my site, now it stopped working since FB updated the graph API. Anyone has a workaround for this? Apparently it now requires an appkey, but the site is not an app, nor is the page I am trying to get the feed from, so what now? Thanks!

    Kobus

    Posted June 13, 2011 at 9:21 pm | Permalink
  • Your demo does not seem to work any more. I suspect that this is because you now need an authorization token to access the feed.

    Posted June 15, 2011 at 5:23 pm | Permalink
  • broke

    The demo don’t seem to work, so I guess this doesn’t work anymore?

    Posted June 16, 2011 at 9:19 am | Permalink
  • fran2tek

    Does not work for me. It return this json object :
    jsonp1308940357967({
    “error”: {
    “type”: “OAuthException”,
    “message”: “An access token is required to request this resource.”
    }
    });

    It seem i need the access token. How can your code work ?
    Any idea ?

    Posted June 24, 2011 at 7:38 pm | Permalink
  • shaolin46

    Hi…I’ve some problems :(
    If I write in the firebug console (the url is that in the FB documentation) :
    $.getJSON(‘https://graph.facebook.com/19292868552?callback=?‘); end execute…I can’t see anything!

    /the

    Posted June 25, 2011 at 6:04 pm | Permalink
  • john

    Thanks.. saved my day.

    Posted June 29, 2011 at 4:25 pm | Permalink
  • !!!!
    !!!!
    not working – mayby facebook change API????!!!!!!!!!!!

    Posted July 4, 2011 at 1:13 pm | Permalink
  • saved my day toooo ;) !!! nice

    Posted July 12, 2011 at 1:40 pm | Permalink
  • tarek

    Hey this thing isn working with me any help

    Posted July 13, 2011 at 7:02 pm | Permalink
  • Thanks but this code does not work…

    Posted July 21, 2011 at 9:13 am | Permalink
  • Yep Facebook have changed their api! – I will be updating the post soon.

    Posted July 28, 2011 at 7:30 pm | Permalink
  • Any idea when the new tutorial will be available? It seems to work if I simply copy your access token from the demo page but I’m sure I’ll run in to problems later if I do that…

    Posted August 16, 2011 at 1:21 pm | Permalink
  • Hi Andreas, Very busy in the studio at the moment. We will get on it ASAP.
    Not a good idea copying and pasting our access_token. It’s insecure and also will expire.
    Thats the access_token for the Pretty Klicks facebook app.

    Thanks,
    Stefan

    Posted August 16, 2011 at 1:24 pm | Permalink
  • Hey Benji, I used your plugin and it’s awesome! Any way to add events as well?

    Posted August 28, 2011 at 4:28 am | Permalink
  • Rob Butz

    I’m really looking forward to you updating the tutorial if it’s still in the cards. Also, I’d love to know how to parse out Graph field data from a Facebook page itself, such as “likes,” not just the feed. Thanks for providing this, though!

    Posted September 18, 2011 at 12:58 pm | Permalink
  • Hey guys, I managed to integrate this quite nicely into my website by looking at your source code of your demo and tweaking it to work to my liking, except for one thing. Would any of you know how to integrate the time and date of the post as it shows on the facebook page too?

    Thanks alot :)

    Posted November 1, 2011 at 3:54 am | Permalink
  • Rick

    You have XSS vulnerabilities in your code sample, and now everyone has copied it. Oops.

    So what happens if fb.message is a script tag and inside the script tags is some nasty javascript?

    Posted January 6, 2012 at 9:58 pm | Permalink
  • Hi Rick, thanks for this, you are correct. However, this will only be a major issue in two circumstances. 1) If the feed is coming from a facebook page not within control of the user / website who is displaying the feed. or 2) The users facebook page is hacked. Thanks for raising this as educational purposes this is a classic and good example. We will be re-writing this tutorial soon anyway to incorporate the work around for getting the access token. Do you work in security?

    Posted January 6, 2012 at 10:06 pm | Permalink
  • I have double checked and users who post on the specified facebook page wall also appear which does pose a risk. Looking into a solution to recommend users with. Any suggestions welcome to resolve this asap. Thanks again for raising this!

    Posted January 6, 2012 at 10:34 pm | Permalink
  • I have now updated the blog post with a fix that should prevent XSS attacks, I used a jQuery method as we are using the jQuery library anyway. Thanks again to Rick!

    Posted January 6, 2012 at 11:05 pm | Permalink

3 Trackbacks

  1. By Our first Jquery plugin of many on November 3, 2010 at 1:36 am

    [...] does this called Tweet there isn’t anything for Facebook. Inspired by the code written by Pretty Klicks we wrote a simple little plugin called fbStatus. Plans in the next revision will be to more of the [...]

  2. By How to read Facebook Graph API feed using JQuery on March 22, 2011 at 2:56 am

    [...] hopefully this code was helpful in your Facebook development. Special thanks to PrettyKlicks for the inspiration to write this. Enjoy! Share and [...]

  3. [...] Append your Facebook Graph url (example link from PrettyKlicks JSON Facebook [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe
Keep up to date with our latest posts.

Learn more about making the most of your website, marketing and seo. It takes two seconds to subscribe to our rss feed.
Contact
You can get in contact with us via phone or e-mail, we will get back to you very promptly.

0141 416 2221
Call you back?
If you would like us to call you back, just pop your number in below and we will get back to you very soon!

Copyright © 2005 - 2012 Pretty Klicks (prettyklicks.com)