无法在Laravel 4中使用Facebook的PHP SDK获取用户个人资料

时间:2014-08-09 05:20:22

标签: php facebook laravel facebook-php-sdk facebook-sdk-4.0

我是使用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]=&gt;
  int(400)
  ["rawResponse":"Facebook\FacebookRequestException":private]=&gt;
  string(86) "{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}"
  ["responseData":"Facebook\FacebookRequestException":private]=&gt;
  array(1) {
    ["error"]=&gt;
    array(3) {
      ["message"]=&gt;
      string(27) "Invalid OAuth access token."
      ["type"]=&gt;
      string(14) "OAuthException"
      ["code"]=&gt;
      int(190)
    }
  }
  ["message":protected]=&gt;
  string(27) "Invalid OAuth access token."
  ["string":"Exception":private]=&gt;
  string(0) ""
  ["code":protected]=&gt;
  int(190)
  ["file":protected]=&gt;
  string(77) "/app/www/vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequestException.php"
  ["line":protected]=&gt;
  int(104)
  ["trace":"Exception":private]=&gt;
  array(19) {
    [0]=&gt;
    array(6) {
      ["file"]=&gt;
      string(68) "/app/www/vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequest.php"
      ["line"]=&gt;
      int(268)
      ["function"]=&gt;
      string(6) "create"
      ["class"]=&gt;
      string(33) "Facebook\FacebookRequestException"
      ["type"]=&gt;
      string(2) "::"
      ["args"]=&gt;
      array(3) {
        [0]=&gt;
        string(86) "{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}"
        [1]=&gt;
        object(stdClass)#159 (3) {
          ["message"]=&gt;
          string(27) "Invalid OAuth access token."
          ["type"]=&gt;
          string(14) "OAuthException"
          ["code"]=&gt;
          int(190)
        }
        [2]=&gt;
        int(400)
      }
    }
    [1]=&gt;
    array(6) {
      ["file"]=&gt;
      string(47) "/app/www/app/controllers/FacebookController.php"
      ["line"]=&gt;
      int(26)
      ["function"]=&gt;
      string(7) "execute"
      ["class"]=&gt;
      string(24) "Facebook\FacebookRequest"
      ["type"]=&gt;
      string(2) "-&gt;"
      ["args"]=&gt;
      array(0) {
      }
    }
    [2]=&gt;
    array(4) {
      ["function"]=&gt;
      string(7) "getUser"
      ["class"]=&gt;
      string(18) "FacebookController"
      ["type"]=&gt;
      string(2) "-&gt;"
      ["args"]=&gt;
      array(0) {
      }
    }
    [3]=&gt;
    array(4) {
      ["file"]=&gt;
      string(71) "/app/www/vendor/laravel/framework/src/Illuminate/Routing/Controller.php"
      ["line"]=&gt;
      int(231)
      ["function"]=&gt;
      string(20) "call_user_func_array"
      ["args"]=&gt;
      array(2) {
        [0]=&gt;
        &amp;array(2) {
          [0]=&gt;
          object(FacebookController)#128 (3) {
            ["beforeFilters":protected]=&gt;
            array(0) {
            }
            ["afterFilters":protected]=&gt;
            array(0) {
            }
            ["layout":protected]=&gt;
            NULL
          }
          [1]=&gt;
          string(7) "getUser"
        }
        [1]=&gt;
        &amp;array(0) {
        }
      }
    }
    [4]=&gt;
    array(6) {
      ["file"]=&gt;
      string(31) "/app/www/bootstrap/compiled.php"
      ["line"]=&gt;
      int(5784)
      ["function"]=&gt;
      string(10) "callAction"
      ["class"]=&gt;
      string(29) "Illuminate\Routing\Controller"
      ["type"]=&gt;
      string(2) "-&gt;"
      ["args"]=&gt;
      array(2) {
        [0]=&gt;
        &amp;string(7) "getUser"
        [1]=&gt;
        &amp;array(0) {
        }
      }
    }
    [5]=&gt;
    array(6) {
      ["file"]=&gt;
      string(31) "/app/www/bootstrap/compiled.php"
      ["line"]=&gt;
      int(5772)
      ["function"]=&gt;
      string(4) "call"
      ["class"]=&gt;
      string(39) "Illuminate\Routing\ControllerDispatcher"
      ["type"]=&gt;
      string(2) "-&gt;"
      ["args"]=&gt;
      array(3) {
        [0]=&gt;
        object(FacebookController)#128 (3) {
          ["beforeFilters":protected]=&gt;
          array(0) {
          }
          ["afterFilters":protected]=&gt;
          array(0) {
          }
          ["layout":protected]=&gt;
          NULL
        }
        [1]=&gt;
        object(Illuminate\Routing\Route)#143 (8) {
          ["uri":protected]=&gt;
          string(52) "facebook/user/{one?}/{two?}/{three?}/{four?}/{five?}"
          ["methods":protected]=&gt;
          array(2) {
            [0]=&gt;
            string(3) "GET"
            [1]=&gt;
            string(4) "HEAD"
          }
          ["action":protected]=&gt;
          array(3) {
            ["uses"]=&gt;
            object(Closure)#144 (2) {
              ["static"]=&gt;
              array(2) {
                ["d"]=&gt;
                object(Illuminate\Routing\ControllerDispatcher)#137 (2) {
                  ["filterer":protected]=&gt;
                  object(Illuminate\Routing\Router)#109 (14) {
                    ["events":protected]=&gt;
                    object(Illuminate\Events\Dispatcher)#14 (5) {
                      ["container":protected]=&gt;
                      object(Illuminate\Foundation\Application)#2 (16) {
                        ["booted":protected]=&gt;
                        bool(true)
                        ["bootingCallbacks":protected]=&gt;
                        array(0) {
                        }
                        ["bootedCallbacks":protected]=&gt;
                        array(2) {
                          [0]=&gt;
                          object(Closure)#74 (2) {
                            ["static"]=&gt;
                            array(2) {
                              ["app"]=&gt;
                              *RECURSION*
                              ["me"]=&gt;
                              object(Illuminate\View\ViewServiceProvider)#67 (2) {
                                ["app":protected]=&gt;
                                *RECURSION*
                                ["defer":protected]=&gt;
                                bool(false)
                              }
                            }
                            ["this"]=&gt;
                            object(Illuminate\View\ViewServiceProvider)#67 (2) {
                              ["app":protected]=&gt;
                              *RECURSION*
                              ["defer":protected]=&gt;
                              bool(false)
                            }
                          }
                          [1]=&gt;
                          object(Closure)#46 (1) {
                            ["static"]=&gt;
                            array(2) {
                              ["app"]=&gt;
                              *RECURSION*
                              ["env"]=&gt;
                              string(10) "production"
                            }
                          }
                        }
                        ["finishCallbacks":protected]=&gt;
                        array(0) {
                        }
                        ["shutdownCallbacks":protected]=&gt;
                        array(0) {
                        }
                        ["middlewares":protected]=&gt;
                        array(0) {
                        }
                        ["serviceProviders":protected]=&gt;
                        array(11) {
                          [0]=&gt;
                          object(Illuminate\Events\EventServiceProvider)#11 (2) {
                            ["app":protected]=&gt;
                            *RECURSION*
                            ["defer":protected]=&gt;
                            bool(false)
                          }
                          [1]=&gt;
                          object(Illuminate\Exception\ExceptionServiceProvider)#15 (2) {
                            ["app":protected]=&gt;
                            *RECURSION*
                            ["defer":protected]=&gt;
                            bool(false)
                          }
                          [2]=&gt;
                          object(Illuminate\Routing\RoutingServiceProvider)#26 (2) {
                            ["app":protected]=&gt;
                            *RECURSION*
                            ["defer":protected]=&gt;
                            bool(false)
                          }
                          [3]=&gt;
                          object(Illuminate\Cookie\CookieServiceProvider)#48 (2) {
                            ["app":protected]=&gt;
                            *RECURSION*
                            ["defer":protected]=&gt;
                            bool(false)
                          }
                          [4]=&gt;
                          object(Illuminate\Database\DatabaseServiceProvider)#51 (2) {
                            ["app":protected]=&gt;
                            *RECURSION*
                            ["defer":protected]=&gt;
                            bool(false)
                          }
                          [5]=&gt;
                          object(Illuminate\Encryption\EncryptionServiceProvider)#56 (2) {
                            ["app":protected]=&gt;
                            *RECURSION*
                            ["defer":protected]=&gt;
                            bool(false)
                          }
                          [6]=&gt;
                          object(Illuminate\Filesystem\FilesystemServiceProvider)#59 (2) {
                            ["app":protected]=&gt;
                            *RECURSION*
                            ["defer":protected]=&gt;
                            bool(false)
                          }
                          [7]=&gt;
                          object(Illuminate\Session\SessionServiceProvider)#62 (2) {
                            ["app":protected]=&gt;
                            *RECURSION*
                            ["defer":protected]=&gt;
                            bool(false)
                          }
                          [8]=&gt;
                          object(Illuminate\View\ViewServiceProvider)#67 (2) {
                            ["app":protected]=&gt;
                            *RECURSION*
                            ["defer":protected]=&gt;
                            bool(false)
                          }
                          [9]=&gt;
                          object(Illuminate\Workbench\WorkbenchService…

1 个答案:

答案 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());
}