问题描述
然而,我是 Laravel 的新手,我正在关注 http 上的教程://www.codeanchor.net/blog/complete-laravel-socialite-tutorial/,通过 Facebook 将用户登录到我的应用程序.然而,几乎在任何地方,我都可以找到使用 Github 或 Twitter 来制作 Laravel 中提供的 Socialite 插件的教程.
I am new to Laravel however and I am following the tutorial on http://www.codeanchor.net/blog/complete-laravel-socialite-tutorial/, to login a user through Facebook into my application. However, almost everywhere I find a tutorial using either Github or Twitter for the Socialite Plugin provided in Laravel.
我的问题是,按照教程中的所有内容,当我点击登录 Facebook"按钮时,它会抛出一个无效参数异常",并且没有指定 Socialite 驱动程序.
My problem is that on following everything in the tutorial, as I click on the "Login to Facebook" button, it throws an "Invalid Argument Exception" with No Socialite driver was specified.".
另一个堆栈溢出问题似乎缩小了范围:https://stackoverflow.com/questions/29673898/laravel-socialite-invalidargumentexception-in-socialitemanager-php-line-138-n
Another stack overflow question seemed to narrow things down:https://stackoverflow.com/questions/29673898/laravel-socialite-invalidargumentexception-in-socialitemanager-php-line-138-n
说明问题出在 config/services.php
Stating that the problem is in the config/services.php
现在,我有了 app_id 和 app_secret.但是,重定向链接似乎令人困惑,因为我在 Facebook 上也找不到它.我知道这是我的应用程序应该去 Facebook 登录的地方,但是,不确定它应该是什么.
Now, i have the app_id and app_secret. However, the redirect link seems to be confusing as I can't find it on Facebook either. I am aware that this is where my app should go to Facebook for login, however, unsure of what it should be.
有没有人对此有任何想法.
Does anyone have any idea on this.
推荐答案
在你的 composer.json 中添加- "laravel/socialite": "~2.0",
In your composer.json add- "laravel/socialite": "~2.0",
"require": {
"laravel/framework": "5.0.*",
"laravel/socialite": "~2.0",
运行composer update
在 config/services.php 添加:
//Socialite
'facebook' => [
'client_id' => '1234567890444',
'client_secret' => '1aa2af333336fffvvvffffvff',
'redirect' => 'http://laravel.dev/login/callback/facebook',
],
你需要创建两条路线,我的是这样的:
You need to create two routes, mine are like these:
//Social Login
Route::get('/login/{provider?}',[
'uses' => 'AuthController@getSocialAuth',
'as' => 'auth.getSocialAuth'
]);
Route::get('/login/callback/{provider?}',[
'uses' => 'AuthController@getSocialAuthCallback',
'as' => 'auth.getSocialAuthCallback'
]);
您还需要为上面的路由创建控制器,如下所示:
You also need to create controller for the routes above like so:
<?php namespace AppHttpControllers;
use LaravelSocialiteContractsFactory as Socialite;
class AuthController 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';
}
}
}
最后将站点 URL 添加到您的 Facebook 应用程序,如下所示:
and finally add Site URL to your Facebook App like so:
这篇关于使用 Laravel Socialite 登录 Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!