php 使用 Laravel Socialite 登录 Facebook
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30590243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Laravel Socialite to login to facebook
提问by Namit
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.
然而,我是 Laravel 的新手,我正在关注http://www.codeanchor.net/blog/complete-laravel-socialite-tutorial/上的教程,通过 Facebook 将用户登录到我的应用程序中。然而,几乎在任何地方,我都能找到使用 Github 或 Twitter 的教程,用于 Laravel 中提供的 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.".
我的问题是,按照教程中的所有内容,当我单击“登录 Facebook”按钮时,它会抛出一个“无效参数异常”,并且没有指定 Socialite 驱动程序。”。
Another stack overflow question seemed to narrow things down: https://stackoverflow.com/questions/29673898/laravel-socialite-invalidargumentexception-in-socialitemanager-php-line-138-n
另一个堆栈溢出问题似乎缩小了范围:https: //stackoverflow.com/questions/29673898/laravel-socialite-invalidargumentexception-in-socialitemanager-php-line-138-n
Stating that the problem is in the config/services.php
说明问题出在 config/services.php
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.
现在,我有了 app_id 和 app_secret。但是,重定向链接似乎令人困惑,因为我在 Facebook 上也找不到它。我知道这是我的应用程序应该去 Facebook 登录的地方,但是,不确定它应该是什么。
Does anyone have any idea on this.
有没有人对此有任何想法。
回答by Emeka Mbah
In your composer.json add- "laravel/socialite": "~2.0",
在您的 composer.json 添加- "laravel/socialite": "~2.0",
"require": {
"laravel/framework": "5.0.*",
"laravel/socialite": "~2.0",
the run composer update
运行 composer update
In config/services.phpadd:
在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 App\Http\Controllers;
use Laravel\Socialite\Contracts\Factory 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';
}
}
}
and finally add Site URL to your Facebook App like so:
最后将站点 URL 添加到您的 Facebook 应用程序,如下所示:
回答by chebaby
Update 2018 - laravel 5.6 - socialite 3.0
2018 年更新 - laravel 5.6 - 社交名流 3.0
It's little bit tricky for something that looks/should be easy, but anyway this is how i make things works for me.
对于看起来/应该很容易的事情来说有点棘手,但无论如何这就是我让事情对我有用的方式。
Server side
服务器端
you can find those instructions and more details in socialite docs
您可以在社交名流文档中找到这些说明和更多详细信息
Installation
安装
composer require laravel/socialite
Configuration
配置
in config/services.phpadd
在config/services.php添加
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('FACEBOOK_CALLBACK_URL'),
],
in .envfile add
在.env文件中添加
FACEBOOK_CLIENT_ID=paste_client_id_here
FACEBOOK_CLIENT_SECRET=paste_client_secret_here
FACEBOOK_CALLBACK_URL=https://www.example.com/auth/facebook/callback
in routes/web.phpadd
在routes/web.php添加
Route::get('auth/facebook/', 'Auth\FacebookController@redirect')->name('auth.facebook');
Route::get('auth/facebook/callback', 'Auth\FacebookController@callback')->name('auth.facebook.callback');
in App\Http\Controllers\Authadd new controller FacebookController.php
在App\Http\Controllers\Auth添加新控制器FacebookController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use Socialite;
class FacebookController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
/**
* Create a new controller instance.
*
* @return void
*/
public function callback(Request $request)
{
try {
$facebookAccount = Socialite::driver('facebook')->user();
// your logic here...
return redirect()->route('your.route.name');
} catch (Exception $e) {
return redirect()->route('auth.facebook');
}
}
}
Facebook side
脸书端
go to https://developers.facebook.com/appsand create new app (if you don't have one already)
转到https://developers.facebook.com/apps并创建新应用程序(如果您还没有)
and make sur your app settings are like below in those screen shots:
并确保您的应用设置在这些屏幕截图中如下所示:
Important note
重要的提示
If you are developing in your local machine, you have to use tools like ngrokthat provide a secure link to your localhost.
如果您在本地机器上开发,则必须使用像ngrok这样的工具来提供到本地主机的安全链接。
In the facebook login settings change https://www.example.comwith the urlprovided by ngroksomething like https://8b0215bc.ngrok.io.
在 Facebook 登录设置中,使用ngrok提供的url更改https://www.example.com,例如https://8b0215bc.ngrok.io。
It is the only way that worked for me while developing in my local machine.
这是在我的本地机器上开发时对我有用的唯一方法。
回答by Khan Shahrukh
Create a provider under your config/services.php file
在您的 config/services.php 文件下创建一个提供程序
'facebook' => [
'client_id' => 'your-fb-client-id',
'client_secret' => 'your-fb-secret',
'redirect' => 'http://your-redirect.com/route',
],
now you can create a controller with following code
现在您可以使用以下代码创建控制器
//this function will redirect users to facebook login page
public function facebook()
{
return \Socialize::with('facebook')->redirect();
}
public function callback()
{
$user = \Socialize::with('facebook')->user();
//now we have user details in the $user array
dd($user);
}
and this is your route
这是你的路线
Route::get('facebook', 'LoginController@facebook');
Route::get('callback', 'LoginController@callback');