Reputation: 6234
I am trying to retrieve user's facebook profile. I am using Laravel 4 framework. Bur I am getting "Invalid oauth access token" every time.
My code is given below.
<?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;
class FacebookController extends BaseController{
public function __construct(){
session_start();
FacebookSession::setDefaultApplication('xx','yy');
}
//when the user clicks on the login button he comes to this function
public function getLogin(){
$helper = new FacebookRedirectLoginHelper(url('facebook/user'));
return Redirect::to($helper->getLoginUrl());
}
//when the user logs in, he comes to this function
public function getUser(){
try{
$session = new FacebookSession(Session::get('_token'));//I believe here lies the problem
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
dd($graphObject);
} catch(Exception $e){
dd($e);
}
}
}
Upvotes: 0
Views: 890
Reputation: 320
Please, take a look at the working code we are testing at http://valoresdovale.com/login/fb:
<?php
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookAuthorizationException;
use Facebook\FacebookRequestException;
session_start(); //this can be moved to app/public/index.php
Route::get('/', function()
{
return View::make('hello');
});
Route::get('login/fb', function() {
FacebookSession::setDefaultApplication('YOUR_ID', 'YOUR_SECRET');
$helper = new FacebookRedirectLoginHelper('http://yoururl/login/fb/callback');
$loginUrl = $helper->getLoginUrl();
return "<a href='$loginUrl'>facebook</a>";
});
Route::get('login/fb/callback', function() {
// A FacebookResponse is returned from an executed FacebookRequest
$helper = new FacebookRedirectLoginHelper('http://yoururl/login/fb/callback');
$session = $helper->getSessionFromRedirect();
$request = new FacebookRequest($session, 'GET', '/me');
try {
$response = $request->execute();
$me = $response->getGraphObject();
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
print_r($me);//->getProperty("id"));
$user = new Giftee;
$user->name = $me->getProperty('first_name') . ' ' . $me->getProperty('last_name');
$user->email = $me->getProperty('email') . "@facebook.com.br";
$user->photo = 'https://graph.facebook.com/' . $me->getProperty('id') . '/picture?type=large';
$user->save();
});
This was based on http://maxoffsky.com/code-blog/integrating-facebook-login-into-laravel-application/ and updated to the new Facebook SDK by Denisson Leal.
Upvotes: 1
Reputation: 1046
You don't need session_start()
, laravel will handle all of that session management for you.
It doesn't seem like a laravel-specific problem, but like a simple error in your code:
The example on facebook's documentation page uses FacebookRedirectLoginHelper::getSessionFromRedirect()
to retrieve the session with which to handle the request(s).
$helper = new FacebookRedirectLoginHelper();
try {
$session = $helper->getSessionFromRedirect();
// your old code
}
Upvotes: 2