欢迎访问web学习网
当前位置:主页 > PHP > 正文

laravel 通过socialite集成微信qq微博登陆

02-08 PHP

1.通过composer安装如下的依赖
 
composer require laravel/socialite 2.0.21
composer require socialiteproviders/manager 2.2.1
composer require socialiteproviders/weibo 2.0.1
composer require socialiteproviders/qq 2.0.5
composer require socialiteproviders/weixin 3.0.0
 
国外文档
https://packagist.org/packages/laravel/socialite
https://packagist.org/packages/socialiteproviders
 
2.配置
 
在config/app.php中的providers和aliases数组加入如下设置
'providers' => [
 
     SocialiteProviders\Manager\ServiceProvider::class,
],
 
'aliases' => [
 
   'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],
 
在config/services.php中添加微信qq微博的一些配置参数
 'qq' => [
        'client_id' => '',
        'client_secret' => '',
        'redirect' => '',
    ],
 
  'weixin' => [
        'client_id' => '',
        'client_secret' => '',
        'redirect' => '',
  ],
 
 'weibo' => [
        'client_id' => '',
        'client_secret' => '',
        'redirect' => '',
  ],
 
在app/Providers/EventServiceProvider.php的listen数组添加
protected $listen = [
    'SocialiteProviders\Manager\SocialiteWasCalled' => [
      'SocialiteProviders\Weibo\WeiboExtendSocialite@handle',
      'SocialiteProviders\QQ\QqExtendSocialite@handle',
      'SocialiteProviders\Weixin\WeixinExtendSocialite@handle',      
     ],
 ];
 
3.使用(以微博为例)
 
<?php
 
namespace App\Http\Controllers\Sns;
 
use Socialite;
 
class WeiboController extends Controller
{
    
    public function login()
    {
       return Socialite::driver('weibo')->redirect();
    }
 
    public function loginCallback()
    {
        $user = Socialite::driver('weibo')->user();
        dd($user);
    }
}


如果需要重写配置可以这样

$clientId = "secret"; 
$clientSecret = "secret"; 
$redirectUrl = "http://yourdomain.com/api/redirect"; 
$config = new \SocialiteProviders\Manager\Config($clientId, $clientSecret, $redirectUrl); 
return Socialite::with('weibo')->setConfig($config)->redirect();
 


文章来源: 本站
打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦