简体   繁体   English

无法与流明一起使用Laravel / Socialite

[英]Unable to use Laravel / Socialite with Lumen

I try to use the package Laravel\\Socialite in my system in Lumen (5.1) 我尝试在流明 (5.1)的系统中使用软件包Laravel \\ Socialite

I added this in the config\\services.php file : 我将此添加到config\\services.php文件中:

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

In bootstrap\\app.php file : bootstrap\\app.php文件中:

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

Then I created a controller for the facebook authentication : 然后我创建了一个用于facebook身份验证的控制器:

<?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 : 并在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 : 当我转到页面http://local.dev/admin/facebook/login ,出现以下错误:

Non-static method Laravel\\Socialite\\Contracts\\Factory::driver() cannot be called statically, assuming $this from incompatible context 非静态方法Laravel \\ Socialite \\ Contracts \\ Factory :: driver()不能被静态调用,假设$ this来自不兼容的上下文

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 无法实例化界面Laravel \\ Socialite \\ Contracts \\ Factory

How do you make this module to work? 您如何使该模块正常工作?

here's how that works in my case 这就是我的情况

in services.php file 在services.php文件中

 '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. 还要检查您的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. 这是auth控制器方法。 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 我的Facebook应用程序 fb_settings

giga.com:8000 is my localhost (so its the same localhost:8000) giga.com:8000是我的本地主机(因此它是相同的本地主机:8000)

fb_settings2

fb_settings3

as you can see in Valid OAuth redirect URI, you should put there your callback. 正如您在有效OAuth重定向URI中看到的那样,您应该在其中放置回调。 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 如果您在localhost上工作,则应在config / services.php中命名您的域,如下所示

'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. 重新启动服务器,清除浏览器cookie。 hope it helps 希望能帮助到你

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. 我仍然准确地说,我的答案仅与Lumen有关 ,因为该文档是为Laravel创建的。

They tell us to create a config\\services.php file and put in the application passwords. 他们告诉我们创建config\\services.php文件并输入应用程序密码。 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: 但是,您必须在bootstrap\\app.php文件中添加标识符,如下所示:

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

Verification : The facebook method in Laravel\\Socialite\\SocialiteManager.php file : 验证: Laravel\\Socialite\\SocialiteManager.php文件中的facebook方法:

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

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

This code call app > config > services . 此代码调用app > config > services So the configuration above is correct. 所以上面的配置是正确的。 After test, all work ! 经过测试,一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM