Reputation: 6234
I'm an app using Facebook's new PHP 4 SDK. I am trying to login using Facebook. I am using Laravel 4. But there seems to be something wrong and I can't figure it out.
Here is my code for the controller.
<?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('xxx','yyy');
}
//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'));
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
dd($graphObject);
} catch(Exception $e){
dd($e);
}
}
}
This is error I am getting.
object(Facebook\FacebookAuthorizationException)#160 (10) {
["statusCode":"Facebook\FacebookRequestException":private]=>
int(400)
["rawResponse":"Facebook\FacebookRequestException":private]=>
string(86) "{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}"
["responseData":"Facebook\FacebookRequestException":private]=>
array(1) {
["error"]=>
array(3) {
["message"]=>
string(27) "Invalid OAuth access token."
["type"]=>
string(14) "OAuthException"
["code"]=>
int(190)
}
}
["message":protected]=>
string(27) "Invalid OAuth access token."
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(190)
["file":protected]=>
string(77) "/app/www/vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequestException.php"
["line":protected]=>
int(104)
["trace":"Exception":private]=>
array(19) {
[0]=>
array(6) {
["file"]=>
string(68) "/app/www/vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequest.php"
["line"]=>
int(268)
["function"]=>
string(6) "create"
["class"]=>
string(33) "Facebook\FacebookRequestException"
["type"]=>
string(2) "::"
["args"]=>
array(3) {
[0]=>
string(86) "{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}"
[1]=>
object(stdClass)#159 (3) {
["message"]=>
string(27) "Invalid OAuth access token."
["type"]=>
string(14) "OAuthException"
["code"]=>
int(190)
}
[2]=>
int(400)
}
}
[1]=>
array(6) {
["file"]=>
string(47) "/app/www/app/controllers/FacebookController.php"
["line"]=>
int(26)
["function"]=>
string(7) "execute"
["class"]=>
string(24) "Facebook\FacebookRequest"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[2]=>
array(4) {
["function"]=>
string(7) "getUser"
["class"]=>
string(18) "FacebookController"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[3]=>
array(4) {
["file"]=>
string(71) "/app/www/vendor/laravel/framework/src/Illuminate/Routing/Controller.php"
["line"]=>
int(231)
["function"]=>
string(20) "call_user_func_array"
["args"]=>
array(2) {
[0]=>
&array(2) {
[0]=>
object(FacebookController)#128 (3) {
["beforeFilters":protected]=>
array(0) {
}
["afterFilters":protected]=>
array(0) {
}
["layout":protected]=>
NULL
}
[1]=>
string(7) "getUser"
}
[1]=>
&array(0) {
}
}
}
[4]=>
array(6) {
["file"]=>
string(31) "/app/www/bootstrap/compiled.php"
["line"]=>
int(5784)
["function"]=>
string(10) "callAction"
["class"]=>
string(29) "Illuminate\Routing\Controller"
["type"]=>
string(2) "->"
["args"]=>
array(2) {
[0]=>
&string(7) "getUser"
[1]=>
&array(0) {
}
}
}
[5]=>
array(6) {
["file"]=>
string(31) "/app/www/bootstrap/compiled.php"
["line"]=>
int(5772)
["function"]=>
string(4) "call"
["class"]=>
string(39) "Illuminate\Routing\ControllerDispatcher"
["type"]=>
string(2) "->"
["args"]=>
array(3) {
[0]=>
object(FacebookController)#128 (3) {
["beforeFilters":protected]=>
array(0) {
}
["afterFilters":protected]=>
array(0) {
}
["layout":protected]=>
NULL
}
[1]=>
object(Illuminate\Routing\Route)#143 (8) {
["uri":protected]=>
string(52) "facebook/user/{one?}/{two?}/{three?}/{four?}/{five?}"
["methods":protected]=>
array(2) {
[0]=>
string(3) "GET"
[1]=>
string(4) "HEAD"
}
["action":protected]=>
array(3) {
["uses"]=>
object(Closure)#144 (2) {
["static"]=>
array(2) {
["d"]=>
object(Illuminate\Routing\ControllerDispatcher)#137 (2) {
["filterer":protected]=>
object(Illuminate\Routing\Router)#109 (14) {
["events":protected]=>
object(Illuminate\Events\Dispatcher)#14 (5) {
["container":protected]=>
object(Illuminate\Foundation\Application)#2 (16) {
["booted":protected]=>
bool(true)
["bootingCallbacks":protected]=>
array(0) {
}
["bootedCallbacks":protected]=>
array(2) {
[0]=>
object(Closure)#74 (2) {
["static"]=>
array(2) {
["app"]=>
*RECURSION*
["me"]=>
object(Illuminate\View\ViewServiceProvider)#67 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
}
["this"]=>
object(Illuminate\View\ViewServiceProvider)#67 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
}
[1]=>
object(Closure)#46 (1) {
["static"]=>
array(2) {
["app"]=>
*RECURSION*
["env"]=>
string(10) "production"
}
}
}
["finishCallbacks":protected]=>
array(0) {
}
["shutdownCallbacks":protected]=>
array(0) {
}
["middlewares":protected]=>
array(0) {
}
["serviceProviders":protected]=>
array(11) {
[0]=>
object(Illuminate\Events\EventServiceProvider)#11 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
[1]=>
object(Illuminate\Exception\ExceptionServiceProvider)#15 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
[2]=>
object(Illuminate\Routing\RoutingServiceProvider)#26 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
[3]=>
object(Illuminate\Cookie\CookieServiceProvider)#48 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
[4]=>
object(Illuminate\Database\DatabaseServiceProvider)#51 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
[5]=>
object(Illuminate\Encryption\EncryptionServiceProvider)#56 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
[6]=>
object(Illuminate\Filesystem\FilesystemServiceProvider)#59 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
[7]=>
object(Illuminate\Session\SessionServiceProvider)#62 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
[8]=>
object(Illuminate\View\ViewServiceProvider)#67 (2) {
["app":protected]=>
*RECURSION*
["defer":protected]=>
bool(false)
}
[9]=>
object(Illuminate\Workbench\WorkbenchService…
Upvotes: 0
Views: 851
Reputation: 46
I'm just going to guess its the same issue I had using the new Facebook PHP SDK. the issue you're proablly having is the storeState and loadState methods in the FacebookRedirectLoginHelper use different PHP session methods than the Laravel Session.
Here is what I did to resolve that issue. You basically have to create your own class and extend FacebookRedirectLoginHelper
something like this.
<?php namespace Ninjaparade\Fbuser\Repo;
use Session;
class NPFacebookRedirectLoginHelper extends \Facebook\FacebookRedirectLoginHelper
{
protected function storeState($state)
{
Session::put('state', $state);
}
protected function loadState()
{
return $this->state = Session::get('state');
}
}
then you can do the following in your login method.
public function getLogin(){
$helper = new NPFacebookRedirectLoginHelper(url('facebook/user'));
return Redirect::to($helper->getLoginUrl());
}
Upvotes: 1