所以我绝对不能接受这个。我正在关注 Laravel 5.2 教程。
http://blog.damirmiladinov.com/laravel/laravel-5.2-socialite-facebook-login.html#.V2gUIrgrJPY
并出现标题中列出的错误。我的路线如下:
Route::get('/', function () {
if(Auth::check()) return view('auth/register');
return view('auth/login');
});
Route::get('/redirect', 'MailAuthController@redirect');
Route::get('/callback', 'MailAuthController@callback');
控制器看起来像这样:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Socialite;
class MailAuthController extends Controller
{
//
public function redirect()
{
return \Socialite::with('microsoft')->redirect();
}
public function callback()
{
// when microsoft calls with token
}
public function user()
{
}
}
services.php 看起来像这样:
<?php
return [
/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Stripe, Mailgun, Mandrill, and others. This file provides a sane
| default location for this type of information, allowing packages
| to have a conventional place to find your various credentials.
|
*/
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
'mandrill' => [
'secret' => env('MANDRILL_SECRET'),
],
'ses' => [
'key' => env('SES_KEY'),
'secret' => env('SES_SECRET'),
'region' => 'us-east-1',
],
'sparkpost' => [
'secret' => env('SPARKPOST_SECRET'),
],
'stripe' => [
'model' => App\User::class,
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
'microsoft' => [
'client_id' => env('MICROSOFT_CLIENT_ID'),
'client_secret' => env('MICROSOFT_CLIENT_SECRET'),
'redirect' => env('http://localhost:8000/callback'),
],
];
除此之外,我不知道我可能会在哪里出错。照亮我的路!
我建议使用 Socialite Providers 包中的 Microsoft Graph 提供程序。
通过您的
composer.json
文件拉入 Microsoft-Graph 提供程序:
"require": {
...
"laravel/socialite": "^2.0",
"socialiteproviders/microsoft-graph": "dev-master"
},
运行
composer update
。
接下来,将连接凭据添加到
config/services.php
:
...
'graph' => [
'client_id' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'client_secret' => 'xxxxxxxxxxxxxxxxxxxxxxx',
'redirect' => 'https://my-app.dev',
],
*注意:如果将
config/services.php
提交到公共存储库,请将这些值提取到您的 .env 文件并通过 env 帮助器方法引用它们;
在
config/app.php
中将 SocialiteProviders/Generators
服务提供者添加到 providers
数组中:
'providers' => [
...
/*
* Package Service Providers...
*/
Laravel\Socialite\SocialiteServiceProvider::class,
// This is a dependency of the socialiteproviders/microsoft-graph provider, and will be installed with the provider via it's composer.json file
SocialiteProviders\Manager\ServiceProvider::class,
注册
Socialize
外观(也在 config/app.php
中):
'aliases' => [
...
'Socialize' => 'Laravel\Socialite\Facades\Socialite',
],
在
app/Providers/EventServiceProvider.php
中注册事件监听器:
protected $listen = [
...
'SocialiteProviders\Manager\SocialiteWasCalled' => [
'SocialiteProviders\Graph\GraphExtendSocialite@handle'
],
];
创建控制器来处理请求:
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Socialize;
class AuthController extends \App\Http\Controllers\Controller
{
/**
* Redirect the user to the Graph authentication page.
*
* @return Response
*/
public function redirectToProvider()
{
return Socialize::with('graph')->redirect();
}
/**
* Obtain the user information from graph.
*
* @return Response
*/
public function handleProviderCallback(Request $request)
{
$user = Socialize::with('graph')->user();
// $user->token;
}
}
最后在
routes/web.php
中添加你的路线:
<?php
Route::get('auth/graph', 'Auth\AuthController@redirectToProvider');
Route::get('auth/graph/callback','Auth\AuthController@handleProviderCallback');
如果有人仍然遇到同样的错误,但已经使用 SocialiteProviders Microsoft 提供商:
检查您是否正确设置了库。
socialiteproviders/microsoft
config/providers.php
:\SocialiteProviders\Manager\ServiceProvider::class
app/Providers/EventServiceProvider.php
:
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
[SocialiteProviders\Microsoft\MicrosoftExtendSocialite::class, 'handle'],
],
最后一步很重要,是什么导致了我的错误,因为我不明白事件侦听器是必需的(而不仅仅是扩展提供程序的可选方式)。
所以这可能看起来很明显,但就我而言,从问题中提供的信息来看,这一步也缺失了。我从当前的 Microsoft 更改为 Graph,仍然遇到相同的错误,但是我后来意识到当驱动程序未在服务提供商中注册时会发生此错误。确保您在供应商中使用与服务提供商相同的拼写,并且在我的例子中包含服务提供商:
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* The event to listener mappings for the application.
*
* @var array<class-string, array<int, class-string>>
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
/*--- I forgot this --->*/\SocialiteProviders\Graph\GraphExtendSocialite::class.'@handle',
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
//
}
/**
* Determine if events and listeners should be automatically discovered.
*
* @return bool
*/
public function shouldDiscoverEvents()
{
return false;
}
}
这添加了与 microsoft graph 一起使用的功能,驱动程序:“graph”来自:https://github.com/SocialiteProviders/Microsoft-Graph
我从未尝试过在socialiteproviders.com上列出的驱动程序“Microsoft” 截至撰写本文时,图表已从该网站上删除,但我关心的是它是否有效并且按预期工作!
有时编译的事件和代码会导致问题。 清除 Laravel 缓存并重试:
php artisan clear-compiled
php artisan event:clear
php artisan route:clear
php artisan optimize:clear