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:
|
// Construct a Facebook object with your app id and secret $facebook = new Facebook($client_id, $client_secret); // Get the authorization url. $perms is an array of extended permission. See config.php $url = $facebook->getAuthorizeUrl($callback_url, $perms); // Redirect the user to that url header("Location: {$url} "); |
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 :
|
$facebook = new Facebook($client_id, $client_secret); $data = $facebook->getAccessToken($callback_url, $code); $facebook->setAccessToken($data['access_token']); $response = $facebook->call_api("/me/feed", "post", "message=Hello, Facebook!"); print_r($response); |
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!