0

I'm trying to make a login system using facebook SDK, because later I'll need some user's data. On my login page blade I put this code:

session_start();
...
$fb = new Facebook\Facebook([
  'app_id' => 'Facebook_id',
  'app_secret' => 'Facebook_secret',
  'default_graph_version' => 'v2.2',
  ]);

$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl("miwebsite.com/callback", $permissions);

OK. This works good. Then I put this on my callback controller:

session_start();
          $fb = new Facebook\Facebook([
          'app_id' => 'Facebook_id',
          'app_secret' => 'Facebook_secret',
          'default_graph_version' => 'v2.2',
          ]);
          $helper = $fb->getRedirectLoginHelper();
          try {
            $accessToken = $helper->getAccessToken();
          } catch(Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: ' . $e->getMessage();
            exit;
          } catch(Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
          }

          if (isset($accessToken)) {
            // Logged in!
            $_SESSION['facebook_access_token'] = (string) $accessToken;
            echo $_SESSION['facebook_access_token'];
            // Now you can redirect to another page and use the
            // access token from $_SESSION['facebook_access_token']
          }

And I get this error:

Class 'App\Facebook\Facebook' not found

But if I put the exact code (Copy-pasted) on the view (callback.blade.php) it works O_o

My question is. How can I fix it to work on the controller instead of the view? Because later I'm gonna need to retrieve some data and I need to make this works on controllers... :(

Any clue will help :)

about_robots
  • 71
  • 1
  • 9
  • Try to use a best solutions like: [socialite](http://stackoverflow.com/questions/30590243/using-laravel-socialite-to-login-to-facebook) It's works fine. – mcklayin Oct 22 '15 at 10:23
  • Socialite works fine when you only need to autentificate the user. But in my case, I nees to retrieve some user's profile. That's why I'm using Facebook SDK – about_robots Oct 22 '15 at 10:27
  • If you need only user profile, you can retrive it with socialite – mcklayin Oct 22 '15 at 10:31
  • 1
    Your controller is namespaced, so you will need to do: `$fb = new \Facebook\Facebook();` – craig_h Oct 22 '15 at 10:48
  • Thank you so much. Thar worked perfectly craig. You're my hero right now :D – about_robots Oct 22 '15 at 12:52

0 Answers0