Using the Facebook Graph API

I caught viral fever and decided to take the day off from work. To pass my leisure I decided to play with the Facebook APIs. I noticed that Facebook Platform has changed a lot. They have released the new “php-sdk” to let the devlopers integrate facebook into their own sites. Actually, they have released SDKs for other languages too (namely Python and JS). For playing with the toys at hand, I picked up the php-sdk and started browsing through the docs. Amazingly, Facebook has launched OAuth support. I am very happy about that. I no longer need to run my applications inside the facebook frame. Rather I can customize my contents for the facebook users. This is plain cool!

I downloaded the php-sdk and started off. But soon, I faced problem. The php-sdk is not well documented (in my opinion) and I had a very hard time figuring things out. It became even worse when I failed to test some of the features which are tightly integrated deep inside facebook. For example, I couldn’t find a way to construct some advanced URLs and also the sdk was not suitable for my debugging needs. The sdk is not poor, I later found it out to be strong but the main problem is it’s not much documented.

So, to try out things and to figure out how the Facebook OAuth works, I started writing my own codes. That helped though I struggled a bit to setup permissions. When I saw that the code is working fine, I chose to pack it up in a class file and rewrite the testing environment.

It took me 30 minutes to rewrite everything up. The package now contains the library and a console to test different methods.

You can download it from: http://masnun.googlecode.com/files/facebook-graph_API-oauth.zip

Extract it into a directory on your website. Register a facebook application. Go through the traditional setup. Please remember to add your domain name on the Connect tab of the application you created 🙂 Edit the config.php and visit the index.php using your browser. You’ll be redirected to Facebook without a word. When you authorize and return, you get a fine console to test things out and see for yourself! 😀

Lets see a quick intro to the library (facebook.php) 🙂

Facebook requires you to redirect the user to an authorization URL with your client id and the callback url. So, we first do this:

You have to ask for permissions on Authorization. I couldn’t get to make the user extend permissions on the post authorization phase. So, I have included **ALL** available permissions in the config.php. Permission is what I suffered with most. If you use my library, it’ll ask the user for all permissions so that you can test everything out. I know you won’t need all those permissions in production environment. Just comment out the ones you don’t want!

Facebook returns the user to the $callback_url with a GET parameter named “code” with others. By using this code with the application secret and callback url, we can get the access_token for the user 🙂 We can do that easily using the client :

getAccessToken() retrieves the access_token from the facebook server. setAccessToken() adds the token to the facebook object and makes it ready to make api calls.

The $data is an array with the keys – access_token and expires. After we set the access token, we can make API calls from the client. For the details on API Paths, please visit: http://developers.facebook.com/docs/api 🙂

I really liked the new structure of the facebook API. The previous API was bloated and sometimes very hard to implement. I do thank Facebook very much for making things easy for the developers!


11 Comments Using the Facebook Graph API

  1. tania

    this is phantastic! before i found this i spent hours trying to figure out how to grant extended permissions! thank you!

    Reply
  2. james

    Really.. Nice Demo…

    Can you please show the code to update status of user through access_token .which I got from the all above code….

    Nice work so far… Thanks Again

    Reply
  3. Ian

    The line: $response = $facebook->call_api(“/me/feed”, “post”, “message=Hello, Facebook!”);

    works for posting that basic message. I thought this would do a message with more detail, but it failed me:

    $attachment = array(
    ‘message’ => ‘Test Post :’,
    ‘name’ => “This is the title of my post”,
    ‘link’ => “http://www.example.com/”,
    ‘description’ => “and a 1 and a 2 and a 3″,
    ‘picture’=>””,
    );

    $response = $facebook->call_api(“/me/feed”, “post”, $attachment);

    How can I send the other parameters? Thanks for you postings!

    Reply
  4. Ian

    Another question: is your class meant to be a substitute for, or a supplement to, the facebook php sdk?

    Reply
  5. maSnun

    It’s meant to be neither of them. I wrote it when I couldn’t find enough docs on the php-sdk and couldn’t use it properly. You would be better off with the official php-adk.

    Reply
  6. Ian

    Thanks for the info. I must say your class was the first one to really open up the techniques available with the new API.

    Reply
  7. Nirav

    Good try for facebook integration. Do you have ever tried with replying message(From Inbox/Mailbox) using facebook API. I would like to know about that API name only if you know. I have tried a lot for this but I think facebook does not provide API for replying message. Some information , I got from the following link :

    http://techcrunch.com/2009/08/11/facebook-launches-one-way-mailbox-api-woo-hoo/

    So , If you know about this issue , let me know.

    Thanks in advanced..

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *