Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

sns_auth

> 安全
Open source

通用第三方登录SDK,支持微信,微信扫码,QQ,微博登录,支付宝登录,Facebook,Line,Twitter,Google

618 stars0 likes3 views
WebsiteGitHub

About

通用第三方登录SDK,支持微信,微信扫码,QQ,微博登录,支付宝登录,Facebook,Line,Twitter,Google

通用第三方登录说明文档

2.0 版本全新发布,目前支持的登录平台包括:

  • 微信
  • QQ
  • 微博
  • 支付宝
  • Facebook
  • Twitter
  • Line
  • Google

安装

composer require anerg2046/sns_auth

类库使用的命名空间为\\anerg\\OAuth2

目录结构

.
├── README.md                        说明文件
├── composer.json                    composer文件
├── src                              代码源文件目录
│   ├── Connector
│   │   ├── Gateway.php              必须继承的抽象类
│   │   └── GatewayInterface.php     必须实现的接口
│   ├── Gateways
│   │   ├── Alipay.php
│   │   ├── Facebook.php
│   │   ├── Google.php
│   │   ├── Line.php
│   │   ├── Qq.php
│   │   ├── Twitter.php
│   │   ├── Weibo.php
│   │   └── Weixin.php
│   ├── Helper
│   │   └── Str.php                  字符串辅助类
│   └── OAuth.php                    抽象实例类
└── wx_proxy.php                     微信多域名代理文件

公共方法

在接口文件中,定义了 4 个方法,是每个第三方基类都必须实现的,用于相关的第三方登录操作和获取数据。方法名如下:

php
    /**
     * 得到跳转地址
     */
    public function getRedirectUrl();

    /**
     * 获取当前授权用户的openid标识
     */
    public function openid();

    /**
     * 获取格式化后的用户信息
     */
    public function userinfo();

    /**
     * 获取原始接口返回的用户信息
     */
    public function userinfoRaw();

微信有一个额外的方法,用于获取代理请求的地址

php
    /**
     * 获取中转代理地址
     */
    public function getProxyURL();

典型用法

以 ThinkPHP5 为例

…

2.0 版本不再通过系统自动设置 state,如有需要请自行处理验证,state 也放入 config 里即可

Line 和 Facebook 强制要求传递 state,如果你没有设置,则会传递随机值

如果要验证 state,则在获取用户信息的时候要加上->mustCheckState()方法。

php
$snsInfo = OAuth::$name($this->config)->mustCheckState()->userinfo();

注意,不是所有的平台都支持传递 state,请自行阅读官方文档

客户端登录

…

配置文件样例

1.微信

'app_id'     => 'wxbc4113c******',
'app_secret' => '4749970d284217d0a**********',
'scope'      => 'snsapi_userinfo',//如果需要静默授权,这里改成snsapi_base,扫码登录系统会自动改为snsapi_login

2.QQ

'app_id'        => '1013****',
'app_secret'    => '67c52bc284b32e7**********',
'scope'         => 'get_user_info',

QQ 现在可以获取unionid了,详见: http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D 只需要配置参数$config['withUnionid'] = true,默认不会请求获取 Unionid

3.微博

'app_id'     => '78734****',
'app_secret' => 'd8a00617469018d61c**********',
'scope'      => 'all',

4.支付宝

'app_id'      => '2016052*******',
'scope'       => 'auth_user',
'pem_private' => Env::get('ROOT_PATH') . 'pem/private.pem', // 你的私钥
'pem_public'  => Env::get('ROOT_PATH') . 'pem/public.pem', // 支付宝公钥

5.Facebook

'app_id'     => '2774925********',
'app_secret' => '99bfc8ad35544d7***********',
'scope'      => 'public_profile,user_gender',//user_gender需要审核,所以不一定能获取到

facebook 有个特殊的配置$config['field'],默认是'id,name,gender,picture.width(400)',你可以根据需求参考官方文档自行选择要获取的用户信息

6.Twitter

'app_id'     => '3nHCxZgcK1WpYV**********',
'app_secret' => '2byVAPayMrG8LISjopwIMcJGy***************',

7.Line

'app_id'     => '159******',
'app_secret' => '1f19c98a61d148f2************',
'scope'      => 'profile',

8.Google

'app_id'     => '7682717*******************.apps.googleusercontent.com',
'app_secret' => 'w0Kq-aYA***************',
'scope'      => 'https://www.googleapis.com/auth/userinfo.profile',

返回样例

Array
(
    [openid] => 1047776979*******
    [channel] => google
    [nick] => Coeus Rowe
    [gender] => m //twitter和line不会返回性别,所以这里是n,Facebook根据你的权限,可能也不会返回,所以也可能是n
    [avatar] => https://lh6.googleusercontent.com/-iLps1iAjL8Q/AAAAAAAAAAI/AAAAAAAAAAA/Bu5l0EIquF0/photo.jpg
)

微信会返回特有的 unionid 字段

其他

使用中如果有什么问题,请提交 issue,我会及时查看

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPalipayfacebookgoogleline

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category安全
PricingOpen source

> Related tools

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.