简体   繁体   English

Laravel / Socialite:类Laravel \\ Socialite \\ Contracts \\ Factory不存在

[英]Laravel/Socialite: Class Laravel\Socialite\Contracts\Factory does not exist

I'm trying to implement socialite but I am getting an error relating to the Factory class. 我正在尝试实施社交名媛,但遇到与Factory类有关的错误。 My app can not find it. 我的应用找不到。

This is the code in my controller: 这是我的控制器中的代码:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Laravel\Socialite\Contracts\Factory as Socialite;

class PortalController extends Controller
{

    public function __construct(Socialite $socialite){
           $this->socialite = $socialite;
       }


    public function getSocialAuth($provider=null)
    {
       if(!config("services.$provider")) abort('404'); //just to handle providers that doesn't exist

       return $this->socialite->with($provider)->redirect();
    }


    public function getSocialAuthCallback($provider=null)
    {
       if($user = $this->socialite->with($provider)->user()){
          dd($user);
       }else{
          return 'something went wrong';
       }
    }

I added: 我补充说:

Laravel\\Socialite\\SocialiteServiceProvider::class, to providers and Laravel\\Socialite\\SocialiteServiceProvider::class,提供给

'Socialite' => Laravel\\Socialite\\Facades\\Socialite::class to aliases 'Socialite' => Laravel\\Socialite\\Facades\\Socialite::class为别名

and my routes looks like 我的路线看起来像

Route::get('/portal/{provider?}',[
        'uses' => 'PortalController@getSocialAuth',
        'as'   => 'portal.getSocialAuth'
    ]);


    Route::get('/portal/callback/{provider?}',[
        'uses' => 'PortalController@getSocialAuthCallback',
        'as'   => 'portal.getSocialAuthCallback'
    ]);

The error I receive is: 我收到的错误是:

ReflectionException in Container.php line 798: Class Laravel\\Socialite\\Contracts\\Factory does not exist Container.php第798行中的ReflectionException:类Laravel \\ Socialite \\ Contracts \\ Factory不存在

I also came across this issue in Laravel 5.5 while creating custom oAuth provider . 在创建自定义oAuth提供程序时,我也在Laravel 5.5遇到了此问题。 After Long research I achieved by creating custom MySocialServiceProvider class whcih is need to extend by Laravel\\Socialite\\SocialiteServiceProvider . 经过长时间的研究,我通过创建自定义MySocialServiceProvider类实现了这一点,需要通过Laravel\\Socialite\\SocialiteServiceProvider进行扩展。 Please go through all the following code and setup with appropriate config, surely it will work. 请仔细阅读以下所有代码,并使用适当的配置进行设置,确保它可以正常工作。

My Directory structure as following in the image 我的目录结构如下图所示 在此处输入图片说明

MySocialServiceProvider.php MySocialServiceProvider.php

<?php

namespace App\Providers;

use Laravel\Socialite\SocialiteServiceProvider;

class MySocialServiceProvider extends SocialiteServiceProvider
{
    public function register()
    {
        $this->app->bind('Laravel\Socialite\Contracts\Factory', function ($app) {
            return new MySocialManager($app);
        });
    }
}

We have to create a Manger class which will contain as follows 我们必须创建一个Manger类,其中将包含以下内容

MySocialManager.php MySocialManager.php

<?php

namespace App\Providers;

use App\Auth\SocialiteFooDriver;
use Laravel\Socialite\SocialiteManager;

class MySocialManager extends SocialiteManager
{
    protected function createFooDriver()
    {
        $config = $this->app['config']['services.foo'];

        return $this->buildProvider(
            SocialiteFooDriver::class, $config
        );
    }
}

We should create a Custom Driver which used by MySocialManger 我们应该创建一个MySocialManger使用的自定义驱动程序

SocialiteFooDriver.php SocialiteFooDriver.php

<?php

namespace App\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;

class SocialiteFooDriver extends AbstractProvider implements ProviderInterface
{
    /**
     * Foo API endpoint.
     *
     * @var string
     */
//    protected $apiUrl = 'https://auth.foobar.com';
    protected $apiUrl = '';

    public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl)
    {
        parent::__construct($request, $clientId, $clientSecret, $redirectUrl);
        $this->apiUrl = config('services.foo.url');
    }

    /**
     * The scopes being requested.
     *
     * @var array
     */
    protected $scopes = ['openid email profile user_role user_full_name'];

    /**
     * {@inheritdoc}
     */
    protected function getAuthUrl($state)
    {
        return $this->buildAuthUrlFromBase($this->apiUrl.'/oauth2/authorize', $state);
    }

    /**
     * {@inheritdoc}
     */
    protected function getTokenUrl()
    {
        return $this->apiUrl.'/oauth2/token';
    }

    /**
     * {@inheritdoc}
     */
    protected function getUserByToken($token)
    {
        $userUrl = $this->apiUrl.'/oauth2/UserInfo?access_token='.$token;

        $response = $this->getHttpClient()->get(
            $userUrl, $this->getRequestOptions()
        );

        $user = json_decode($response->getBody(), true);

        if (in_array('user:email', $this->scopes)) {
            $user['email'] = $this->getEmailByToken($token);
        }

        return $user;
    }

    /**
     * Get the POST fields for the token request.
     *
     * @param string $code
     *
     * @return array
     */
    protected function getTokenFields($code)
    {
        return array_add(
            parent::getTokenFields($code), 'grant_type', 'authorization_code'
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function mapUserToObject(array $user)
    {
        return (new User())->setRaw($user)->map([
            'id' => $user['sub'],
            'nickname' => $user['preferred_username'],
            'name' => Arr::get($user, 'name'),
            'email' => Arr::get($user, 'email'),
            'avatar' => $user['avatar'],               

        ]);
    }

    /**
     * Get the default options for an HTTP request.
     *
     * @return array
     */
    protected function getRequestOptions()
    {
        return [
            'headers' => [
                //'Accept' => 'application/vnd.github.v3+json',
            ],
        ];
    }
}

Finally we should have add config values in config/services.php 最后,我们应该在config / services.php中添加配置值

'foo' => [
        'client_id' => 'XXXXXXXX',
        'client_secret' => 'YYYYYYYY',
        'redirect' => 'http://example.com/login/foo/callback/',
        'url' => 'https://auth.foobar.com',
    ],

Dont forget to update config/app.php with our new provider 不要忘记使用我们的新提供程序更新config / app.php

'providers' => [
//...

 \App\Providers\MySocialServiceProvider::class

]

From the doc , after adding Socialite library and facade to the respective providers and aliases array in config/app.php file, you just need to use Socialite as 文档中 ,将Socialite libraryfacade添加到config/app.php文件中的相应providersaliases数组之后,您只需要使用社交网站作为

use Socialite;

But you are using 但是你在用

use Laravel\Socialite\Contracts\Factory as Socialite;

So, just remove above line with 因此,只需删除上面的行

use Socialite;

updated from comment 已从评论更新

composer update

and

composer dump-autoload

It should work. 它应该工作。

Official installation guide says you need to use this: 官方安装指南说您需要使用此:

use Socialite;

Instead of: 代替:

use Laravel\Socialite\Contracts\Factory as Socialite;

If it's not working, try to use: 如果不起作用,请尝试使用:

use Laravel\Socialite\Facades\Socialite

And then use composer dumpauto . 然后使用composer dumpauto

“ composer update”为我解决了此问题,它可以与“将Laravel \\ Socialite \\ Contracts \\ Factory用作Socialite”一起使用。

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

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