PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 27, 2022

[FIXED] Cannot get user profile using Facebook's PHP SDK in Laravel 4

 February 27, 2022     facebook, facebook-php-sdk, facebook-sdk-4.0, laravel, php     No comments   

Issue

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]=&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…

Solution

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


Answered By - Ninjaparade
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing