Unable to use Laravel / Socialite with Lumen

纵饮孤独 提交于 2019-12-29 07:18:25

问题


I try to use the package Laravel\Socialite in my system in Lumen (5.1)

I added this in the config\services.php file :

<?php
//Socialite
'facebook' => [
    'client_id'     => '##################',
    'client_secret' => '##################',
    'redirect'      => 'http://local.dev/admin/facebook/callback',
],

In bootstrap\app.php file :

class_alias(Laravel\Socialite\Facades\Socialite::class, 'Socialite');
$app->register(Laravel\Socialite\SocialiteServiceProvider::class);

Then I created a controller for the facebook authentication :

<?php

namespace App\Http\Controllers\Facebook;

use App\Http\Controllers\Controller;
use Laravel\Socialite\Contracts\Factory as Socialite;

class FacebookController extends Controller
{
  public function redirectToProviderAdmin()
  {
    return Socialite::driver('facebook')->scopes(['manage_pages', 'publish_actions'])->redirect();
  }

  public function handleProviderCallbackAdmin()
  {
    $user = Socialite::driver('facebook')->user();
  }
}

And in the routes.php :

$app->get('/admin/facebook/login', 'App\Http\Controllers\Facebook\FacebookController@redirectToProviderAdmin');
$app->get('/admin/facebook/callback', 'App\Http\Controllers\Facebook\FacebookController@handleProviderCallbackAdmin');

I just followed the documentation, changing according to my needs. When I go to page http://local.dev/admin/facebook/login, I get the following error :

Non-static method Laravel\Socialite\Contracts\Factory::driver() cannot be called statically, assuming $this from incompatible context

Indeed, according to the code, driver function must be instanciate.

EDIT : And if I try to instanciate this class, I get the following error :

Cannot instantiate interface Laravel\Socialite\Contracts\Factory

How do you make this module to work?


回答1:


here's how that works in my case

in services.php file

 'facebook' => [
    'client_id' => '***************',
    'client_secret' => '***************',
    'redirect' => ""
],

i left redirect empty cause my site is multilingual (so, it fills in a bit later with sessions). if you use only one language, put there your callback absolute path. for example

"http://example.com:8000/my-callback/";

also check your config/app.php. in providers array

Laravel\Socialite\SocialiteServiceProvider::class,

in aliases array

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

my routes look like this

Route::get('facebook', 'Auth\AuthController@redirectToProvider');
Route::get('callback', 'Auth\AuthController@handleProviderCallback');

here's auth controllers methods. put in top

use Socialite;
 //იობანი როტ
 public function redirectToProvider(Request $request)
{
    return Socialite::with('facebook')->redirect();
}

 public function handleProviderCallback(Request $request)
  {
    //here you hadle input user data
    $user = Socialite::with('facebook')->user();

  }

my facebook app

giga.com:8000 is my localhost (so its the same localhost:8000)

as you can see in Valid OAuth redirect URI, you should put there your callback. in my case i use three urls cause i have three languages. in your case it should be just

http://your-domain-name.com:8000/callback

if you work on localhost, you should name your domain in config/services.php mine look like this

'domain' => "your-domain.com",

after everything run these commands

php artisan cache:clear
php artisan view:clear
composer dump-autoload

restart your server, clear your browser cookies. hope it helps




回答2:


I found the reason for this bug. Simply not write

use Laravel\Socialite\Contracts\Factory as Socialite;

But

use Socialite;

Then, I again fell on worries. I still accurate that my answer is only associated with Lumen, because the documentation has been made for Laravel.

They tell us to create a config\services.php file and put in the application passwords. Don't do it, because this file will never be called anywhere.

However, you must add the identifiers in the bootstrap\app.php file, like this:

config(['services' => [
  'facebook' => [
    'client_id'     => '##################',
    'client_secret' => '##################',
    'redirect'      => 'http://local.dev/admin/facebook/callback',
  ],
]]);

Verification : The facebook method in Laravel\Socialite\SocialiteManager.php file :

protected function createFacebookDriver()
{
    dd($config = $this->app['config']['services']);

    return $this->buildProvider(
        'Laravel\Socialite\Two\FacebookProvider', $config
    );
}

This code call app > config > services. So the configuration above is correct. After test, all work !



来源:https://stackoverflow.com/questions/35536548/unable-to-use-laravel-socialite-with-lumen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!