thirdparty_oauth
这是一个社会第三方登录扩展包 目前支持 微信、qq、微博、github gitee gitlaba 等第三方登录.
安装
$ composer require pltrue/thirdparty_oauth ^1.0.3
贡献
你可以通过以下三种方式做出贡献:
- bug反馈 issue tracker.
- 回答问题或修复错误 issue tracker.
- 贡献新特性或更新wiki。
兼容
- 支持php >=5.6
如何使用
如何申请应用授权?
参数说明
redirect_url 回调地址将使用方法写到回调接口即可 获取到用户的一些基础信息
client_id 应用授权id
client_secret 应用授权key
建议
前后端分离下建议前端直接请求授权接口,后端负责回调接口即可
php项目中如何使用
授权方法
require __DIR__ .'/vendor/autoload.php';
use Pl1998\ThirdpartyOauth\SocialiteAuth;
$api = new SocialiteAuth([
'client_id' => '74ee75f10437b4862d653a682111e5ddca1d24422f00ec884453ad232ae07ac9',
'redirect_uri' => 'http://oauth.test/test.php'
]);
// 注意格式 github gitee gitlab weibo weixin qq
$json = $api->redirect('weibo');
var_dump($json);
回调接口方法
require __DIR__ .'/vendor/autoload.php';
use Pl1998\ThirdpartyOauth\SocialiteAuth;
$api = new SocialiteAuth([
'client_id' => '74ee75f10437b4862d653a682111e5ddca1d24422f00ec884453ad232ae07ac9',
'redirect_uri' => 'http://oauth.test/test.php',
'client_secret' => ''
]);
// 注意格式 github gitee gitlab weibo weixin qq
$user = $api->driver('gitee')->user();
var_dump($user);die;
在Thinkphp中如何使用?
//在路由文件新建两条路由
Route::get('authorization','api/TestController/authorization')->name('请求授权');
Route::get('gitee/callback','api/TestController/giteeCallback')->name('授权回调接口');
配置文件
return [
// 注意格式 github gitee gitlab weibo weixin qq
'github' => [
'client_id' => '2365a07a73dc25a27e5c7a968248b96beb53a1ad300de7ba6bf4ffe247a4b386',
'redirect_uri' => 'http://test.test/gitee/callback',
'client_secret' => ''
],
'github' => [
'client_id' => '2365a07a73dc25a27e5c7a968248b96beb53a1ad300de7ba6bf4ffe247a4b386',
'redirect_uri' => 'http://test.test/gitee/callback',
'client_secret' => ''
]
];
<?php
/**
* Created by PhpStorm
* User: pl
* Date: 2020/9/17
* Time: 14:56
*/
namespace app\api\controller;
use Pl1998\ThirdpartyOauth\SocialiteAuth;
use think\facade\Config;
class TestController
{
/**
* 该方法重定向后会执行giteeCallback() 方法
* @return int
*/
public function authorization()
{
$auth = new SocialiteAuth(Config::get('aouth.github'));
// 注意格式 github gitee gitlab weibo weixin qq
return $auth->redirect('github');
}
/**
* @throws \Pl1998\ThirdpartyOauth\Exceptions\InvalidArgumentException
*/
public function giteeCallback()
{
$api = new SocialiteAuth(Config::get('aouth.github'));
// 注意格式 github gitee gitlab weibo weixin qq
$user = $api->driver('github')->user();
var_dump($user);
//判断用户是否存在表中 然后存入session 或者颁发token 返回给前端
}
}
在laravel中如何使用?
在laravle的 service.php配置文件中加入配置
.
.
.
// 注意格式 github gitee gitlab weibo weixin qq
'oauth' => [
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'redirect_uri' => env('GITHUB_REDIRECT_URI'),
'client_secret'=>env('GITHUB_CLIENT_SECRET')
]
]
.....
在 .env中配置
GITHUB_CLIENT_ID=xxxx
GITHUB_REDIRECT_URI=xxx
GITHUB_CLIENT_SECRET=xxx
创建路由
Route::get('auth/github','IndexController@auth')->name('github授权');
Route::get('callback/github','IndexController@callback')->name('github回调接口');
控制器方法
/**
* 授权方法
* @return mixed
*/
public function auth()
{
//普通写法
// $auth = new SocialiteAuth(config('services.oauth'));
// $auth->redirect('github');
//laravel 容器使用
app('socialiteAuth')->redirect('github');
}
/**
* 回调方法
*/
public function callback()
{
//普通写法
//$auth = new SocialiteAuth(config('services.oauth.github'));
//$user = $auth->driver('github')->user();
//var_dump($user);
//laravel 容器使用
$user = app('socialiteAuth')->driver('github')->user();
var_dump($user);
}
返回示例
{
"login": "pl1998",
"id": 43993206,
"node_id": "MDQ6VXNlcjQzOTkzMjA2",
"avatar_url": "https://avatars1.githubusercontent.com/u/43993206?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/pl1998",
"html_url": "https://github.com/pl1998",
"followers_url": "https://api.github.com/users/pl1998/followers",
"following_url": "https://api.github.com/users/pl1998/following{/other_user}",
"gists_url": "https://api.github.com/users/pl1998/gists{/gist_id}",
"starred_url": "https://api.github.com/users/pl1998/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/pl1998/subscriptions",
"organizations_url": "https://api.github.com/users/pl1998/orgs",
"repos_url": "https://api.github.com/users/pl1998/repos",
"events_url": "https://api.github.com/users/pl1998/events{/privacy}",
"received_events_url": "https://api.github.com/users/pl1998/received_events",
"type": "User",
"site_admin": false,
"name": "pltrue",
"company": null,
"blog": "pltrue.top",
"location": "深圳",
"email": null,
"hireable": null,
"bio": null,
"twitter_username": null,
"public_repos": 6,
"public_gists": 0,
"followers": 1,
"following": 1,
"created_at": "2018-10-09T12:42:14Z",
"updated_at": "2020-09-17T04:49:23Z"
}
License
MIT