Vote Up 0 Vote Down

integrate facebook login sdk cakephp old version

How to integrate facebook login sdk in cakephp old version ?
flag

Answers


Vote Up 0 Vote Down
Step 1:
Create a facebook app from the facebook developers page. Generate app with public permissions. Download Facebook SDK for php

Step 2 :
Extract it in the folder app/vendors folder

Step 3:
Open the file app/app_controller.php and add the following lines :

session_start(); //Without this an occur will occur
App::import("Vendor", "FacebookAuto", array("file" => "facebook-php-sdk-v4-4.0-dev/autoload.php")); //change your facebook folder name
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

class AppController extends Controller {
//code before ...
public function facebookAuth() {
FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET');
$helper = new FacebookRedirectLoginHelper('http://yoursitename.com/'); //fill the site name as given
try {
$session = $helper->getSessionFromRedirect();
} catch (FacebookRequestException $ex) {
// When Facebook returns an error
echo $ex->getMessage();
} catch (\Exception $ex) {
// When validation fails or other local issues
}
if ($session) {
// Logged in
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject();
$me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
$user = $me->asArray();
$this->set('user', $user);
} else {
$loginUrl = $helper->getLoginUrl();
$this->set('loginUrl', $loginUrl);
}

print_r($user);
}
//other code ...
}


Here you have to change your facebook folder name, give APP ID and SECRET code. Also you have to give the redirect site url as given in the facebook APP page with trailing slashes if given. You will get the facebook profile in the $user variable.
flag | link |

Your Answer

Login before answering

Login with facebook