Saturday, August 8, 2009

Facebook RSS News Feed Reader

So recently I started using a RSS Reader, Google Reader as it happens to be. I like being able to pull together news, slashdot, some sports, etc but it left me still with the occasion for going to Facebook to look at my news feeds of friends. After a little digging I could not find anything that would allow me to have my Facebook News Feeds available in any RSS Feeds.

So, here now exists the Facebook RSS News Feed Reader http://apps.facebook.com/rssnewsfeedreader. This application allows you to view your Facebook News Feed from within your favorite RSS reader.

What is RSS you may ask? RSS stands for Really Simple Syndication and the specification is maintained here http://www.rssboard.org/rss-specification.

Here is a quick overview for how it is done.
First you need to get into the Facebook API a little bit. This is both from the how to setup a Facebook app and also understanding their streams.

- Facebook Getting Started http://developers.facebook.com/get_started.php?tab=tutorial
- Facebook Streams http://wiki.developers.facebook.com/index.php/Stream_%28FQL%29

I developed the application in PHP so here is a little more about how that part works. Basically you have to create a type of proxy so the RSS reader is going to connect to your PHP (or other language based) application which then has to internalize (based on parameters for lets say the session) being passed in.

From here you need to:
1) Setup the session for that user to Facebook in your application
2) Read the profile stream in your application based on the HTTP request
3) Create (by parsing the news feed stream) the RSS XML (make sure you set it up correctly [i.e. having a so each item is uniquely defined]).

Now these 3 steps MUST occur AFTER you have had the user follow these steps to give you the one time authenticator so you can have the infinite session. Navigating the programming parts was pretty straight forward once I got through this with little more understanding about how "Facebook Infinite Session Keys Are NOT Dead!".

To get the infinite session key, you have to go to the following URL, replacing YOUR_API_KEY with your Facebook app’s API key:
http://www.facebook.com/code_gen.php?v=1.0&api_key=YOUR_API_KEY. Once you click "Generate" you will get your one time code (show below as example).







With that in your PHP application you now do this:
$facebook = new Facebook($appapikey, $appsecret);//Create a new facebook object
$infinite_key_array = $facebook->api_client->auth_getSession($authtoken); //$authtoken is the value you got from the above one time step
$infinite_key_array['session_key'] has the value for the session. You can MUST store this (depending on your implementation along with the user id).

Now that you have done this this session id (and the person's user id) is all you need to continue. Now you can setup a session in your PHP app to Facebook and only require some paramaters to be passed in for the RSS feed.

e.g.
$facebook->api_client->user = $_GET["u"];
$facebook->api_client->session_key = $_GET["s"];
$facebook->api_client->expires = 0;

From here, read your stream.
$feed = $facebook->api_client->stream_get();

Loops through your posts from the $feed and create the RSS XML with the proper headers e.g.
header("Content-Type: application/xml; charset=ISO-8859-1");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0", false); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past




/*
Joe Stein
http://www.linkedin.com/in/charmalloc
*/

8 comments:

  1. Thanks for the App, the feed i get is nice but special characters like german ü,ö,ä gert scrambled. Any idea why and how i could chnage that?

    ReplyDelete
  2. I changed the encoding to ISO-8859-1 and uploaded it to the production server. Let me know if this resolved your issue.

    ReplyDelete
  3. Hi,

    I'd like to thank you too for the app.
    I also have an encoding problem (french chars like (è ç & à).

    Can you fix it ?

    ReplyDelete
  4. Is it possible to do this with fan pages?

    ReplyDelete
  5. Hi!

    Thanx a lot for this.
    I can't see Norwegian characters, is it possible to fix easily ?

    Br, Anders

    ReplyDelete
  6. I changed the encoding just now to be utf-8 for the header and xml. Let me know if the problem still exists.

    ReplyDelete
  7. Thank you Joe, its working now :)
    I actually have Facebook updates in my mobile phones home screen (idle screen) now! :)

    ReplyDelete
  8. Great work on this app! Is it possible to get it to update more frequently than 3 hours though? Everything is "real-time" these days.

    Cheers!

    ReplyDelete