Facebook Application From Scratch – 2
This is second part of our Facebook Application Development from Scratch series. You may read part one of the series here. In this part we will learn, how to get authentication for your Facebook Applications. More specifically, we will learn:
- Creating an instance of a PHP Class
- Getting session information of Facebook logged-in user
- Getting User ID of Facebook User
- Getting login URL for authentication purpose
- Using FBML redirect tag
What you need?
In order to develop Facebook Application, we need a client library. Client library is a PHP class which offers us to talk to Facebook APIs in an easy manner. Download Facebook client library from:
http://github.com/facebook/php-sdk/blob/master/src/facebook.php
Start the process!
Create a folder named ‘fb’ in your application folder and move this facebook.php file into the ‘fb’ folder. Create a file fb.inc.php in your ‘fb’ folder and insert below code in it:
<?php
require 'facebook.php';
define('APP_ID','xxxxxxxxxx');
define('SECRET_KEY', 'xxxxxxxxxx');
define('CANVAS_PAGE', 'http://apps.facebook.com/shareacake/');
define('CANVAS_URL', 'http://www.techmug.com/demo/FBFromScratchSeries/');
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => SECRET_KEY,
'cookie' => true,
));
//Get current user's Facebook session
$session = $facebook->getSession();
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($uid) {
//Do nothing
} else {
$params = array(
'next' => CANVAS_PAGE,
'cancel_url' => CANVAS_PAGE,
'fbconnect' => 0,
'canvas' => 1,
'req_perms' => ''
);
$loginUrl = $facebook->getLoginUrl($params);
echo '<fb:redirect url="' . $loginUrl . '"/>';
}
?>
Change Application ID and Secret Key of your own. We got these keys in our first episode of this series. Above code is enough for Facebook Application authentication.
Create a file outside the ‘fb’ folder and name it index.php. Include the above file (fb.inc.php) in this newly created index.php file using below code.
<?php require 'fb/fb.inc.php'; ?>
Finally upload all the files to your server to test the application (At CANVAS_URL).
Code Explanation
In the beginning we include Facebook client library class. After this we define 4 Constants to be used in our application. Then we create an instance of Facebook Class with $facebook object.
After creating an instance, we use our newly created object to get current user’s session by using getSession() method of Facebook Class. If our code finds a session, we try to get current logged-in user’s Facebook ID. We use getUser() method to acquire currently logged-in user’s ID.
At the moment we will not be able to get information about user, because he has not authenticated our application yet. In the result $uid will not be created.
Now, we move forward to authentication process. We check if we have $uid variable available or not. If this variable is not set yet, we try to get the login URL of our application. We start with creating an array named $params. keys inside this array are the hub of our authentication process. Below is the explanation of used keys:
| next | The URL where you want to send user after authentication |
| cancel_url | The URL where you want to send user, if he cancels the process |
| fbconnect | Are you using Facebook Connect? (1 or 0 for Yes and No) |
| canvas | Are you using Facebook canvas? (1 or 0 for Yes and No) |
| req_perms | Additional permissions, you want to access on user account |
For all available additional or extended permissions, please visit – http://developers.facebook.com/docs/authentication/permissions
Now its time to put our $params array in to getLoginUrl() method. At this point we have the login url, where user can actually authenticate our application. We use redirect tag of FBML(Facebook Markup Language) to send the user to authentication page.
At the authentication page, user has two options – Allow and Don’t Allow. Once user clicks on Allow button, our application will be authenticated by user. That was all about getting authentication for your Facebook Application.
Since, our application has been authenticated, we can use different API methods to get needful data. To read more about Facebook API and available methods, please visit – http://developers.facebook.com/docs/api
To see a demo of above code, visit our application at:
http://apps.facebook.com/shareacake/
Once you authenticate the application, you will see some information about you. That’s the thing we will learn in next episode…Cheers!
What’s next?
In the next episode, we will get user data using Facebook API. We will also display some cakes to be shared and we will also talk about that what can be achieved by using Facebook API. So stay tune for your Facebook hunger.
Please feel free to ask, your comments are always welcome. If you like this article, please share it on your favorite website. It may help someone somewhere!




















I tried this code. Working fine except it is not asking for Application Authorization like the old api used to do. It is opening my application directly. No Authorization. :S
You might already have authorized the application. To check it login to your facebook account and go to privacy settings. There you will see Applications and Websites at the bottom section, click there and check if application exist or not.
Thank You. I deleted that application. Opened it next day and the session was not set so I saw that authorization box. Worked fine. I suppose this is how it should work.
Actually the code is working. It was because of Facebook session.
Well problem solved !
while using the above code i am getting following error
Parse error: syntax error, unexpected T_REQUIRE in /home/study117/public_html/newyeargift/fb/fb.inc.php on line 3
can u please suggest on this ??
thanks in advance
Please double check if you are missing any semi-colon at the end of line 3
hi
fb said :
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /home/jeegloc/public_html/techmug.com/demo/FBFromScratchSeries/fb/facebook.php on line 453
?
Where you see this error? I mean can you please explain the steps that you took and this error happened?
[...] Read More Facebook Application From Scratch – 2 [...]