我是使用Facebook全新PHP 4 SDK的应用程序。我正在尝试使用Facebook登录。我正在使用Laravel 4.但似乎有些不对劲,我无法弄明白。
这是我的控制器代码。
<?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);
}
}
}
这是我得到的错误。
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…
答案 0 :(得分:1)
我只是猜测我使用新的Facebook PHP SDK时遇到的问题。 你可以解决的问题是FacebookRedirectLoginHelper中的storeState和loadState方法使用与Laravel Session不同的PHP会话方法。
以下是我为解决该问题所做的工作。你基本上必须创建自己的类并扩展FacebookRedirectLoginHelper
像这样的东西。<?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');
}
}
然后您可以在登录方法中执行以下操作。
public function getLogin(){
$helper = new NPFacebookRedirectLoginHelper(url('facebook/user'));
return Redirect::to($helper->getLoginUrl());
}