Facebook has exposed a lot of its core functionality through Graph API. Third party application developers now can quite easily develop application that live on Facebook's gigantic database of user profile information.But except for the public information , applications are required to authenticate by the user to get access. facebook uses OAuth 2.0 protocol for authentication and authorization and supports a number of different OAuth flows that can be used to authorize and authenticate third party mobile/desktop applications and websites. but regardless of type of the flows, Facebook's OAuth implementation involves 3 steps: user authentication, app authorization and app authentication (more about this can be found in facebook developer site). once these steps are complete, third party application is issued an "user access token" which enables the application to gain access to user infobase on users' behalf. By default, user access token expires after 2 hours which is not good for site owners who want to show their facebook albums etc. on their sites. in that case, site owner has to be careful enough to authenticate once after every 2 hours which is annoying. Guys at Facebook solved this in a way they call "offline_access" permission. user access token with offline access permission doesn't expire. lets take a look at how offline access can be obtained and utilized in your application.
the easiest way to play around with Facebook Graph API is to use SDK. I'll use PHP SDK. First grab the SDK and include into your php file and instantiate SDK's Facebook class.
require_once("facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config['fileUpload'] = false;
$config['cookie'] = true; // don't forget to enable cookie.
$facebook = new Facebook($config);
Once you have Facebook object, you can begin authentication process. Luckily SDK handles all the 3 steps of authentication on your behalf. all you need to do is to initiate the process and SDK will do the rest. As starter, we check if anyone is already logged in or not.
$userId = $facebook->getUser();
if no one is logged in then above statement will return 0 otherwise it will return current user's id.
if($userId) {
try{
// if user is logged in, we try to get his/her profile information
$user = $this->facebook->api('/me', 'GET');
// url you want facebook to redirect after logout
$logouturl = 'YOUR_SITE_URL';
$logoutUrl = $this->facebook->getLogoutUrl(array('next'=>$logouturl));
}
catch(FacebookApiException $e){
//url you want facebook to redirect after login.
// a note of coution though, for security reason you need to inform facebook about this
// URL in your developer profile page.
$loginurl = 'YOUR_SITE_URL';
$params = array(
//This is where you request offline access token
scope => 'user_photos, friends_photos,offline_access',
redirect_uri => $loginurl
);
$loginUrl = $this->facebook->getLoginUrl($params);
}
}
once the authentication process is complete, you can get the access token in the following way:
$accessToken = $facebook->getAccessToken();
You should store the token for use across sessions. You can do the subsequent API calls as :
$facebook->setAccessToken($accessToken);
$facebook->api(/*polymorphic*/);
the easiest way to play around with Facebook Graph API is to use SDK. I'll use PHP SDK. First grab the SDK and include into your php file and instantiate SDK's Facebook class.
require_once("facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config['fileUpload'] = false;
$config['cookie'] = true; // don't forget to enable cookie.
$facebook = new Facebook($config);
Once you have Facebook object, you can begin authentication process. Luckily SDK handles all the 3 steps of authentication on your behalf. all you need to do is to initiate the process and SDK will do the rest. As starter, we check if anyone is already logged in or not.
$userId = $facebook->getUser();
if no one is logged in then above statement will return 0 otherwise it will return current user's id.
if($userId) {
try{
// if user is logged in, we try to get his/her profile information
$user = $this->facebook->api('/me', 'GET');
// url you want facebook to redirect after logout
$logouturl = 'YOUR_SITE_URL';
$logoutUrl = $this->facebook->getLogoutUrl(array('next'=>$logouturl));
}
catch(FacebookApiException $e){
//url you want facebook to redirect after login.
// a note of coution though, for security reason you need to inform facebook about this
// URL in your developer profile page.
$loginurl = 'YOUR_SITE_URL';
$params = array(
//This is where you request offline access token
scope => 'user_photos, friends_photos,offline_access',
redirect_uri => $loginurl
);
$loginUrl = $this->facebook->getLoginUrl($params);
}
}
once the authentication process is complete, you can get the access token in the following way:
$accessToken = $facebook->getAccessToken();
You should store the token for use across sessions. You can do the subsequent API calls as :
$facebook->setAccessToken($accessToken);
$facebook->api(/*polymorphic*/);
No comments:
Post a Comment