我试图在laravel 5中使用oauth谷歌,但我收到了错误.任何人都可以帮我解决这个问题.
以下是我的档案,请查看
.ENV
GOOGLE_ID = 'mygoogleId' GOOGLE_SECRET = 'mysecretID' GOOGLE_REDIRECT = http://localhost:8090/users
services.php
'google' => [ 'client_id' => env('GOOGLE_ID'), 'client_secret' => env('GOOGLE_SECRET'), 'redirect' => env('GOOGLE_REDIRECT'), ], AuthController public function redirectToProvider() { return Socialite::driver('google')->redirect(); } public function handleProviderCallback() { $user = Socialite::driver('google')->user(); console.log($user); }
routes.php文件
Route::get('google', 'Auth\AuthController@redirectToProvider'); Route::get('google/callback', 'Auth\AuthController@handleProviderCallback'); //I have set the providers and aliases in app.php.
//在set()方法上
public function redirect() { $state = str::random(40); if ($this->usesState()) { $this->request->getSession()->set('state', $state); } return new RedirectResponse($this->getAuthUrl($state)); }
提前致谢..
嘿如果您使用laravel 5.2,这对我有用.
将您的控制器放在"web"中间件中.喜欢,
Route::group(['middleware' => 'web'], function() { Route::get('google', 'Auth\AuthController@redirectToProvider'); Route::get('google/callback', 'Auth\AuthController@handleProviderCallback'); });
并确保内核文件已注册中间件类.
/** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \Perkweb\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \Perkweb\Http\Middleware\VerifyCsrfToken::class, ], 'api' => [ 'throttle:60,1', ], ];