i get the following error from FaceBook api:
‘Not Logged In: You are not logged in. Please login and try again.’
I’m using laravel 5, and php-sdk-v4 library so i add the callback page as fucnction in a controller, .
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use Session;
use Facebook;
class FacebookLoginController extends MainController {
public function login() {
$fb = new FacebookFacebook([
'app_id' => '****',
'app_secret' => '****',
'default_graph_version' => 'v2.7',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email'];
$loginUrl = $helper->getLoginUrl('http://localhost/tshop/public/fbcallback', $permissions);
//dd($fb);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}
public function fbcallback() {
$fb = new FacebookFacebook([
'app_id' => '1780077285568634',
'app_secret' => '625dc9eb7561f10bebc79ae2a1c96dca',
'default_graph_version' => 'v2.7',
]);
$helper = $fb->getRedirectLoginHelper();
$_SESSION['FBRLH_state']=$_GET['state'];
try {
$accessToken = $helper->getAccessToken();
} catch (FacebookExceptionsFacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookExceptionsFacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (!isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "n";
echo "Error Code: " . $helper->getErrorCode() . "n";
echo "Error Reason: " . $helper->getErrorReason() . "n";
echo "Error Description: " . $helper->getErrorDescription() . "n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
echo '<pre>';
print_r($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('1780077285568634'); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (!$accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (FacebookExceptionsFacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>nn";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
/* Get user details from facebook */
try {
// Returns a `FacebookFacebookResponse` object
$response = $fb->get('/me?fields=id,name,email', $accessToken);
} catch (FacebookExceptionsFacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookExceptionsFacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
echo '<hr>';
$user = $response->getGraphUser();
echo '<hr>';
echo $user['name'];
}
}
2
Answers
I Add session_start() and it worked! thanks!
it is because OAuth redirect URI is either wrong or it can be if you have added www. in your url,if it is so remove www , simply “http://yoururl ” .
Change this in your facebook app.
I made same mistake .Hope it will work for you.