10

I'm using Socialite to authenticate my users via Facebook. However, I can't get it to work. I followed this tutorial, but I get the following error:

enter image description here

I looked everywhere and tried whatever, but I can't get it to work. Here's my code:

In services.php:

'facebook' => [
    'client_id' => '[My App ID]',
    'client_secret' => '[My App Secret]',
    'redirect' => 'http://localhost:8000/auth/facebook/callback/',
],

My routes:

Route::group(['middleware' => ['web', 'requestlog']], function () {   
    Route::get('auth/facebook', 'Auth\AuthController@redirectToProvider');
    Route::get('auth/facebook/callback/', 'Auth\AuthController@handleProviderCallback');
});

Then in my AuthController:

use Laravel\Socialite\Facades\Socialite;

public function redirectToProvider()
{
    return Socialite::driver('facebook')->redirect();
}

public function handleProviderCallback()
{
    try {

        $providerUser = Socialite::driver('facebook')->user();
        dd('yay it worked!');

      } catch (RequestException $e) {

      dd($e->getResponse()->json());

    }
}

Then I have these settings in FB:

enter image description here

enter image description here

What is going wrong? I followed all the necessary steps as far as I know. I don't get what's wrong here. I hope I provided all the necessary information!

Edit

Here's what the url looks like on the page where the error displays: http://localhost:8000/auth/facebook/callback?code=AQBtnKEqZgImLqN7f3hETe9GptgzFH71sXrV5qmv8Rpo6Oj5-4rl8mBjFPbfkBtiV8w9atV7X4OrWfHyalJkXU-k6lkEv1bly6v5Qxm2es-_RRp8gfoSWOZwjqE34Rvq6__L3aEOERPEa9LSBk_rKVP_cYGZoQeAydRLQUZVGdr_p1SuE1hRZIvZTAZ-zorkPoyyCDNZtDEVFHGRJt_c3kTf_AKE97FVemrXrUDzxaX-rvovKtfGF3u4CvAIt5pe4g7zD30jAWF78ZgjjPpr21MdaGwP5V0tc8g84oz0dR5Nbit7sKeUE-XblWFrQCIKfqs-OJ6rcuzw7iPTx6xrQ9Ev&state=4f924a9974207482c6fce24c1d74705c6688adc0#_=_

I also tried in incognito mode, removed cookies and so on. Same result...

Markinson
  • 2,077
  • 4
  • 28
  • 56
  • Maybe the token is expired, try if you rerequest the token if it works. – Bart Bergmans Dec 08 '16 at 14:17
  • @BartBergmans do you mean the app secret refreshing? If that's not what you mean, then how can I rerequest that token you're talking about? I'm fairly new to this, hope you can help! By the way, I already tried in incognito if that helps. – Markinson Dec 08 '16 at 14:21
  • What will return in this `$providerUser = Socialite::driver('facebook')->user();`? – 502_Geek Dec 12 '16 at 04:17
  • @Markinson `$providerUser` return `null`? – 502_Geek Dec 12 '16 at 04:25
  • @KelvinKyaw The exception gets thrown at `Socialite::driver('facebook')->user()` so `$providerUser` isn't even ever set at all – Markinson Dec 12 '16 at 08:34
  • Can you try to do this in incognito mode? also revoke the access to your app for you facebook user and redo the login process. You are actually being redirected right? Do you get a token in the url when being redirected? Try to manually get the access token using the returned token. – Frnak Dec 13 '16 at 07:50
  • @FrankProvost I tried in incognito, and deleted the app at my facebook profile. I did an update in my post with a little more info. Thanks for your help! – Markinson Dec 13 '16 at 15:29
  • You can take that code that is returned in the url and try to get an access token with it manually. – Frnak Dec 14 '16 at 13:23
  • @FrankProvost That is possible indeed, but I still wonder why it doesn't work the Laravel-way for me... – Markinson Dec 15 '16 at 13:53
  • I'm sadly out of ideas. A good month ago i simply followed the steps from the official laravel docs and everythig worked fine for me. Only suggestion is to quickly setup a fresh laravel installation and try to use the laravel docs instead of the tut to get it running. – Frnak Dec 15 '16 at 14:04
  • @FrankProvost I reïnstalled the entire Socialite package and everything that comes with it and I think i solved the problem! Explained my solution in an answer if you're interested :) – Markinson Dec 16 '16 at 07:53

3 Answers3

10

I didn't want to mess with access tokens or other API's because I don't think that would be a solution to my problem. But now (after I don't know how long...) I got it to work! For whoever has this problem as well; I did the following steps which out of luck made it work:

  1. I deleted socialite from composer.json and updated it.
  2. I reinstalled socialite. I added the following line in composer.json: "laravel/socialite": "2.*".
  3. Then when I ran the snippet I got the following error: ErrorException in FacebookProvider.php line 89:Undefined index: first_name.
  4. After that I replaced the code in FacebookProvider.php with this file from github.

Don't forget to execute random composer dump-autoload's everytime because sometimes that randomly seems to help as well.

Now it finally works for whatever reason!

EDIT

I also noticed that composer update does a good job. Update socialite to the newest version if this doesn't work. This eventually fixed another problem I had recently.

Markinson
  • 2,077
  • 4
  • 28
  • 56
5

Running composer update and then composer dump-autoload solved the issue for me.

I noticed Laravel Socialite was updated, so @Markinson is right

aphoe
  • 2,586
  • 1
  • 27
  • 31
0

For me it worked with composer require laravel/socialite:2.0.21 ... upgrade from 2.0.20