Hi I am using facebook SDKv4 for facebook login on my website
After authentication of app the user is not login into my site neither the details are saving to the database and after authentication i can see the login button
<?php
session_start();
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('APPID','SECRET');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'example.com' );
/***************************************************session handinling*********************************************/
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
//$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
// $session = new FacebookSession( $session->getToken() );
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
/*****************************************************session handling*****************************************/
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fuid= $graphObject->getProperty('id'); // To Get Facebook ID
$fname=$graphObject->getProperty('first_name'); // To Get Facebook full name
$lname=$graphObject->getProperty('last_name'); // To Get Facebook full name
$femail=$graphObject->getProperty('email');
$gender=$graphObject->getProperty('gender');
CODE TO SAVE DETAILS TO DB
/*************************************************************post on users walll******************************************************/
// Publish to User’s Timeline
$request = ( new FacebookRequest( $session, 'POST', '/me/feed', array(
'message' => 'Only TEXT Goes Here'
) ) )->execute();
// Get response as an array, returns ID of post
$response = $request->getGraphObject()->asArray();
print_r( $response );
// Graph API to publish to timeline with additional parameters
$request = ( new FacebookRequest( $session, 'POST', '/me/feed', array(
'name' => 'Name',
'caption' => 'CAtion',
'link' => 'example.com',
'message' => 'msg'
) ) )->execute();
// Get response as an array, returns ID of post
$response = $request->getGraphObject()->asArray();
/****************************************************************post on users wall************************************************/
redirect($site_url);
//echo print_r( $graphObject, 1 );
} else {
// show login url
$permissions = ['email', 'user_likes','publish_actions'];
echo '<img src="'.$img_cdn.'fbcon.png">';
}
?>
Can any one tell where i am doing wrong ?
Related
I've got a problem. My code worked perfectly few days ago. But now it does not. I've changed nothing. Just updated SDK version from 4.0.0 to 4.0.17. Here's my code:
<?php
session_start();
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Foursquare/FoursquareAPI.class.php');
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
$facebookAppKey = 'xxxxxxxxxxxxx';
$facebookSecretKey = 'xxxxxxxxxxxxxx';
FacebookSession::setDefaultApplication( $facebookAppKey, $facebookSecretKey);
$helper = new FacebookRedirectLoginHelper("http://fb.goodnia.com/");
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
$session = new FacebookSession( $_SESSION['fb_token'] );
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
print_r( $ex );
} catch( Exception $ex ) {
print_r( $ex );
}
}
if ( isset( $session ) ) {
$_SESSION['fb_token'] = $session->getToken();
$session = new FacebookSession( $session->getToken() );
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graphObject = $response->getGraphObject();
$email = $graphObject->getProperty('email');
$userlink = $graphObject->getProperty('link');
$name = $graphObject->getProperty('name');
$gender = $graphObject->getProperty('gender');
$identifier = $graphObject->getProperty('id');
print_r($email);
}
?>
Here's the link:
So when i click the link, it redirects me to Facebook authorization page. After inserting my credentials, it redirects me back. But with such params as "code" and "state".
Here it is:
Request URL: http://fb.goodnia.com/?code=AQDe3te_RigftBZoM3GqSto3-SO7d8hMoSGeCFj5i-Js_vB1_X9rb_3N4UoWVSAacY_jbHQf_78E4djrZkYRBxcUvi6cHI8xuH_bknit-f9Fmn5F7s8gaT8Z3i2bKeY-6nGOyRryMVKLqlhGhrDbmp9AshuzmljKOAi06_nC_Ygv2wtwcDEqAbn4dP_8QxCnHitK7tdd0qxhCYVe1LTO_PQrky8lABn2koeLcK5n8l5TS65-obVqJTRI-z_UgZ8feSnOZv3a1fSa2yXocCNHBh5f4lrmrBKnxmf6N47kTXIen4ARmbYNfvmdtlN6ec84mBMz-EZAQUcAWaLuC54zusjz&state=c95ad1b3df3f4044c203029a9b6cac03
Request Method:GET
Status Code: 500 Internal Server Error
Where am I wrong? I appreciate your help.
I am trying to allow a user to log onto my site through the facebook SDK, and so far I have fixed all the errors until this one.
I get an error saying:
Fatal error: Class 'Facebook\HttpClients\FacebookStream' not found in C:\wamp\www\appname\Facebook\HttpClients\FacebookStreamHttpClient.php on line 55
I have re-installed the SDK and the same error is coming up.
my code is:
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/HttpClients/FacebookStreamHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookStreamHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( '******','************' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://localhost/icemes/' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
// show login url
echo 'Login';
}
Thanks!
I am trying to integrate the new FB php SDK version 4.0.9 - everything seems pretty straight forward except one thing which I cannot manage to figure out.
The Facebook login using the Chrome browser needs two tries - I do not know why. I echoed every single step, even monitored my sessions - nothing stands out.
This are the exact steps to reproduce the bug for the code bellow:
log-out from Facebook, close the chrome browser, access the code page, click login, login to Facebook. After being brought back to the redirect page - no session is created.
login again - it works
this problem becomes more frustrating because if I logout from Facebook after a successful login and try again without exiting the browser, everything works fine.
It does not happen with Firefox!
I am using this version: https://developers.facebook.com/docs/php/gettingstarted/4.0.0
!! 4.0.9. extremely different usage than 4.0.0
<?php
session_start();
$appid = 'appid';
$appsecret = 'appsecret';
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookSignedRequestFromInputHelper.php' ); // added in v4.0.9
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
// these two classes required for canvas and tab apps
require_once( 'Facebook/FacebookCanvasLoginHelper.php' );
require_once( 'Facebook/FacebookPageTabHelper.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSignedRequestFromInputHelper; // added in v4.0.9
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// these two classes required for canvas and tab apps
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookPageTabHelper;
// init app with app id and secret
FacebookSession::setDefaultApplication( $appid,$appsecret );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://mypage/code.php' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
echo 'case 0<br/>';
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
echo 'case 1<br/>';
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
echo 'case 2<br/>';
print_r( $ex );
}
}
echo ' avem session? == '.($session ? '1':'0').' == <br/>';
// see if we have a session
if ( isset( $session ) ) {
echo 'case 3<br/>';
// save the session
$_SESSION['fb_token'] = $session->getToken();
if($_SESSION['fb_token']) echo 'avem $_SESSION <br/>';
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
echo 'case 4<br/>';
// show login url
echo 'Login';
}
echo '<br/><br/>';
print_r($_SESSION);
I'm a new developper on Facebook Graph API. I have follow this tutorial : https://www.webniraj.com/2014/05/01/facebook-api-php-sdk-updated-to-v4-0-0. I have test once and it works. I have modify and retest and it's work. But, the next day, nothing... My session token for facebook have change and I don't know why and how.
There is my code :
// ALL includes and variables
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( $app_ID, $app_secret );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( $url );
// generate login url with scope, each permission as element in array
$loginUrl = $helper->getLoginUrl( array($access_right) );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$graphObject = (new FacebookRequest( $session, 'GET', '/me/accounts'))->execute()->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
$page_token = $graphObject['data'][0]->access_token;
echo '<p>Page token : '.$page_token.'</p>';
$session = new FacebookSession($page_token);
/* make the API call */
$request = new FacebookRequest(
$session,
'POST',
'/'.$pageID.'/feed',
array (
'message' => 'This is a test message',
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
// show login url
echo 'Login';
}
My goal is just to put a message on my fan page, and it works the first time. Why the variable "$_SESSION['fb_token']" is now NULL ?? How I can make it work ?
Thanks for response !
Edit 1 :
I discovered that my session have change the CSRF title : 'fb_token' to 'FBRLH_state'. An idea about this change ?
Here is the content of a file I have that logs in users using the latest Facebook PHP-SDK 4.0.9 (I think).
<?php
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
if (!isset($_SESSION['facebookUserId']) || !isset($_SESSION['facebookSession']) || !isset($_SESSION['facebookUserProfile'])) {
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($appId,$appSecret);
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( $canvasUrl );
try {
$FBSession = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
if (!isset($FBSession)) {
// STEP ONE - REDIRECT THE USER TO FACEBOOK FOR AUTO LOGIN / APP APPROVAL
header('Location: ' . $helper->getLoginUrl());
exit();
} else {
$user_profile = (new FacebookRequest($FBSession, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
$_SESSION['facebookUserId'] = $user_profile->getID();
$_SESSION['facebookSession'] = $FBSession; // I DON'T THINK THIS ACTAULLY WORKS RIGHT
$_SESSION['facebookUserProfile'] = $user_profile;
}
}
I simply include this file as seen below.
include ('SimpleFacebookLogin.php');
I try to save the session and it doesn't save properly in the session so I think I am missing something as well but I can successfully log in after creating a session. I am just not sure how to access it again afterward. I hope this helps.
Actualy works! =) Session is saved on my pc with this piece of script.
I added session_start() and session_write_close() - maybe you missed those?
I have updated my facebook api and i'm trying to initiate a session like this,
FacebookSession::setDefaultApplication( 'foo foo','foo foo' );
I get this error on that line,
Fatal error: Class 'facebook\src\Facebook\FacebookSession'not found in /foo/foo/foo/header.php on line 41
My file path is correct...
Any ideas?
Here is my full code,
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// include required files form Facebook SDK
// added in v4.0.5
require_once( 'facebook/src/Facebook/FacebookHttpable.php' );
require_once( 'facebook/src/Facebook/FacebookCurl.php' );
require_once( 'facebook/src/Facebook/FacebookCurlHttpClient.php' );
require_once( 'facebook/src/Facebook/FacebookSession.php' );
require_once( 'facebook/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'facebook/src/Facebook/FacebookRequest.php' );
require_once( 'facebook/src/Facebook/FacebookResponse.php' );
require_once( 'facebook/src/Facebook/FacebookSDKException.php' );
require_once( 'facebook/src/Facebook/FacebookRequestException.php' );
require_once( 'facebook/src/Facebook/FacebookOtherException.php' );
require_once( 'facebook/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'facebook/src/Facebook/GraphObject.php' );
require_once( 'facebook/src/Facebook/GraphSessionInfo.php' );
use facebook\src\Facebook\FacebookHttpable;
use facebook\src\Facebook\FacebookCurl;
use facebook\src\Facebook\FacebookCurlHttpClient;
use facebook\src\Facebook\FacebookSession;
use facebook\src\Facebook\FacebookRedirectLoginHelper;
use facebook\src\Facebook\FacebookRequest;
use facebook\src\Facebook\FacebookResponse;
use facebook\src\Facebook\FacebookSDKException;
use facebook\src\Facebook\FacebookRequestException;
use facebook\src\Facebook\FacebookOtherException;
use facebook\src\Facebook\FacebookAuthorizationException;
use facebook\src\Facebook\GraphObject;
use facebook\src\Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'foo foo','foo foo' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://www.wuno.com/sandbox/' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
} else {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
// show login url
echo 'Login';
}
echo 'sup dude';
?>