laravel 实现微博第三方登陆

https://blog.csdn.net/a12541254/article/details/79415550

1.安装

composer require socialiteproviders/weibo

2.添加 Service Provider

如果之前添加过 Socialite Provider,得先注释掉:
文件 config/app.php

'providers' => [
//    Laravel\Socialite\SocialiteServiceProvider::class,
    SocialiteProviders\Manager\ServiceProvider::class, // add
],

3.添加 Facades Aliase

如果之前安装 Socialite 时添加过,就不需要再添加了。
还是文件 config/app.php

'aliases' => [
    'Socialite' => Laravel\Socialite\Facades\Socialite::class, // add
],

4.添加事件处理器

文件 app/Providers/EventServiceProvider.php

    protected $listen = [
        'SocialiteProviders\Manager\SocialiteWasCalled' => [
            'SocialiteProviders\Weibo\WeiboExtendSocialite@handle',
        ],
    ];

5.添加路由

文件 app/Http/routes.php

// 引导用户到新浪微博的登录授权页面
Route::get('auth/weibo', 'Auth\AuthController@weibo');
// 用户授权后新浪微博回调的页面
Route::get('auth/callback', 'Auth\AuthController@callback');

6.配置

文件 config/services.php

'weibo' => [
    'client_id' => env('WEIBO_KEY'),
    'client_secret' => env('WEIBO_SECRET'),
    'redirect' => env('WEIBO_REDIRECT_URI'),  
],

 

 

 

文件 .env

WEIBO_KEY=App Key

    WEIBO_SECRET=App secret
    WEIBO_REDIRECT_URI=Callback URL

7.到新浪微博申请加入开发者

    http://open.weibo.com/

    如果只是个人练手第三方登陆,可以不进行审核等操作

    在设置回调地址的时候需要设置完全,最好是设置到方法级别。如果配置了域名的话就不必设置完全

8.代码实现

<?php
namespace App\Http\Controllers\User;
 
use App\Http\Controllers\Controller;
use Socialite;
 
 
class UserAuthController extends Controller
{
    
 
    public function weiboSignInProcess(){
        $redirect_url = env('WEIBO_REDIRECT_URI');
        return Socialite::driver('weibo')->scopes(['user_friends'])->redirectUrl($redirect_url)->redirect();
    }
 
    public function weiboSignInCallbackProcess(){
        $redirect_url = env('WEIBO_REDIRECT_URI');
        $userData = Socialite::driver('weibo')->stateless()->user();
        dd($userData);
    }
 
}

以上内容部分转自:

https://segmentfault.com/a/1190000003103229 

http://socialiteproviders.github.io/providers/qq/

==============================后期出现问题=======================================

laravel 中 InvalidArgumentException Driver [WeiBo] not supported.

 

posted @ 2018-08-01 16:25  码农编程进阶笔记  阅读(25)  评论(0编辑  收藏  举报
返回顶部 有事您Q我