wenprise / socialite
从laravel/socialite提取的一系列OAuth 2.0包。
- dev-master
- 3.1.0
- 3.0.1
- 3.0.0
- 2.0.16
- 2.0.15
- 2.0.14
- 2.0.13
- 2.0.12
- 2.0.11
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.27
- 1.0.26
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-develop
This package is auto-updated.
Last update: 2024-09-22 07:51:53 UTC
README
Socialite是一个OAuth2认证工具。它受到laravel/socialite的启发,您可以在任何PHP项目中轻松使用它。
要求
PHP >= 7.0
安装
$ composer require "overtrue/socialite" -vvv
用法
对于Laravel 5: overtrue/laravel-socialite
authorize.php:
<?php use Wenprise\Socialite\SocialiteManager; $config = [ 'github' => [ 'client_id' => 'your-app-id', 'client_secret' => 'your-app-secret', 'redirect' => 'https:///socialite/callback.php', ], ]; $socialite = new SocialiteManager($config); $response = $socialite->driver('github')->redirect(); echo $response;// or $response->send();
callback.php:
<?php use Wenprise\Socialite\SocialiteManager; $config = [ 'github' => [ 'client_id' => 'your-app-id', 'client_secret' => 'your-app-secret', 'redirect' => 'https:///socialite/callback.php', ], ]; $socialite = new SocialiteManager($config); $user = $socialite->driver('github')->user(); $user->getId(); // 1472352 $user->getNickname(); // "overtrue" $user->getUsername(); // "overtrue" $user->getName(); // "安正超" $user->getEmail(); // "anzhengchao@gmail.com" $user->getProviderName(); // GitHub ...
配置
现在我们支持以下网站
facebook,github,google,linkedin,outlook,weibo,taobao,qq,wechat,douyin,以及douban。
每个驱动程序使用相同的配置键:client_id,client_secret,redirect。
示例
...
'weibo' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'https:///socialite/callback.php',
],
...
作用域
在重定向用户之前,您还可以使用范围方法在请求中设置“作用域”。此方法将覆盖所有现有作用域
$response = $socialite->driver('github') ->scopes(['scope1', 'scope2'])->redirect();
重定向URL
您还可以动态设置redirect,您可以使用以下方法更改redirect URL
$socialite->redirect($url); // or $socialite->withRedirectUrl($url)->redirect(); // or $socialite->setRedirectUrl($url)->redirect();
微信作用域
snsapi_base,snsapi_userinfo- 用于媒体平台认证。snsapi_login- 用于网页认证。
附加参数
要将任何可选参数包含在请求中,请使用关联数组调用带有方法的方法
$response = $socialite->driver('google') ->with(['hd' => 'example.com'])->redirect();
用户界面
标准用户API
$user = $socialite->driver('weibo')->user();
{
"id": 1472352,
"nickname": "overtrue",
"name": "安正超",
"email": "anzhengchao@gmail.com",
"avatar": "https://avatars.githubusercontent.com/u/1472352?v=3",
"original": {
"login": "overtrue",
"id": 1472352,
"avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/overtrue",
"html_url": "https://github.com/overtrue",
...
},
"token": {
"access_token": "5b1dc56d64fffbd052359f032716cc4e0a1cb9a0",
"token_type": "bearer",
"scope": "user:email"
}
}
您可以通过以下数组键获取用户属性
$user['id']; // 1472352 $user['nickname']; // "overtrue" $user['name']; // "安正超" $user['email']; // "anzhengchao@gmail.com" ...
或使用以下方法
$user->getId(); $user->getNickname(); $user->getName(); $user->getEmail(); $user->getAvatar(); $user->getOriginal(); $user->getToken();// or $user->getAccessToken() $user->getProviderName(); // GitHub/Google/Facebook...
获取原始OAuth API响应
方法$user->getOriginal()将返回API原始响应的数组。
获取访问令牌对象
您可以通过调用$user->getToken()或$user->getAccessToken()或$user['token']来获取当前会话的访问令牌实例。
使用访问令牌获取用户
$accessToken = new AccessToken(['access_token' => $accessToken]); $user = $socialite->user($accessToken);
自定义会话或请求实例。
在调用driver方法之前,您可以使用自定义的Request实例(其中之一为Symfony\Component\HttpFoundation\Request)设置请求。
$request = new Request(); // or use AnotherCustomRequest. $socialite = new SocialiteManager($config, $request);
或将请求设置为SocialiteManager实例
$socialite->setRequest($request);
您可以通过getRequest()从SocialiteManager实例获取请求
$request = $socialite->getRequest();
设置自定义会话管理器。
默认情况下,SocialiteManager使用Symfony\Component\HttpFoundation\Session\Session实例作为会话管理器,您可以根据以下方式更改它
$session = new YourCustomSessionManager(); $socialite->getRequest()->setSession($session);
您的自定义会话管理器必须实现
Symfony\Component\HttpFoundation\Session\SessionInterface。
享受它! ❤️
参考
- Google - OpenID Connect
- Facebook - Graph API
- Linkedin - 使用OAuth 2.0进行认证
- 微博 - OAuth 2.0 授权机制说明
- QQ - OAuth 2.0 登录QQ
- 微信公众平台 - OAuth文档
- 微信开放平台 - 网站应用微信登录开发指南
- 微信开放平台 - 代公众号发起网页授权
- 豆瓣 - OAuth 2.0 授权机制说明
- 抖音 - 网站应用开发指南
PHP 扩展包开发
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
许可协议
MIT