Yii2 使用 QQ 和 Weibo 第三方登录源码 [ 2.0 版本 ]
我们社区在 yii2-authclient
多次升级后,登录异常。一直想寻求一种通用的方法,尽量不重写 OAuth2
, BaseOAuth
以及 OAuthToken
类, 所以本次直接在 initUserAttributes
方法返回结果的地方去修改,这样会受 yii2-authclient
升级影响较小,我把 QQClient.php
和 WeiboClient.php
放在 frontend/widgets
下了,接下来我们来看代码!
QQClient.php
<?php
namespace frontend\widgets;
use yii\authclient\OAuth2;
use yii\web\HttpException;
use Yii;
class QQClient extends OAuth2
{
public $authUrl = 'https://graph.qq.com/oauth2.0/authorize';
public $tokenUrl = 'https://graph.qq.com/oauth2.0/token';
public $apiBaseUrl = 'https://graph.qq.com';
protected function initUserAttributes()
{
$user = $this->api('user/get_user_info', 'GET', ['oauth_consumer_key' => $this->user->client_id, 'openid' => $this->user->openid]);
return [
'client' => 'qq',
'openid' => $this->user->openid,
'nickname' => $user['nickname'],
'gender' => $user['gender'],
'location' => $user['province'] . $user['city'],
];
}
/**
* @inheritdoc
*/
protected function getUser()
{
$str = file_get_contents('https://graph.qq.com/oauth2.0/me?access_token=' . $this->accessToken->token);
if (strpos($str, "callback") !== false) {
$lpos = strpos($str, "(");
$rpos = strrpos($str, ")");
$str = substr($str, $lpos + 1, $rpos - $lpos -1);
}
return json_decode($str);
}
/**
* @inheritdoc
*/
protected function defaultName()
{
return 'QQ';
}
/**
* @inheritdoc
*/
protected function defaultTitle()
{
return 'QQ 登录';
}
}
WeiboClient.php
<?php
namespace frontend\widgets;
use yii\authclient\OAuth2;
use yii\web\HttpException;
use Yii;
class WeiboClient extends OAuth2
{
public $authUrl = 'https://api.weibo.com/oauth2/authorize';
public $tokenUrl = 'https://api.weibo.com/oauth2/access_token';
public $apiBaseUrl = 'https://api.weibo.com/2';
protected function initUserAttributes()
{
$user = $this->api('users/show.json', 'GET', ['uid' => $this->user->uid]);
return [
'client' => 'weibo',
'openid' => $user['id'],
'nickname' => $user['name'],
'gender' => $user['gender'],
'location' => $user['location'],
];
}
/**
* @inheritdoc
*/
protected function getUser()
{
$str = file_get_contents('https://api.weibo.com/2/account/get_uid.json?access_token=' . $this->accessToken->token);
return json_decode($str);
}
/**
* @inheritdoc
*/
protected function defaultName()
{
return 'Weibo';
}
/**
* @inheritdoc
*/
protected function defaultTitle()
{
return '微博登录';
}
}
╃巡洋艦㊣ 北京
注册时间:2010-11-21
最后登录:1小时前
在线时长:1627小时54分
最后登录:1小时前
在线时长:1627小时54分
- 粉丝1369
- 金钱72633
- 威望835
- 积分97253
热门源码
- 整合完 yii2-rbac+yii2-admin+adminlte 等库的基础开发后台源码
- 适合初学者学习的一款通用的管理后台
- 基于 Yii 2 + Bootstrap 3 搭建一套后台管理系统 CMF
- yii-goaop - 将 goaop 集成到 Yii,在 Yii 中优雅的面向切面编程
- yii-log-target - 监控系统异常且多渠道发送异常信息通知
- 店滴云1.3.0
- 面向对象的一小步:添加 ActiveRecord 的 Scope 功能
- Yii2 开源商城 FecShop
- 基于 Yii2 开发的多店铺商城系统,免费开源 + 适合二开
- leadshop - 基于 Yii2 开发的一款免费开源且支持商业使用的商城管理系统
共 24 条评论
我想拜师 本人白纸一枚
学习了 收藏了
谢谢谢谢了
mark了
mark nice thank you!!!
mark起
顶顶顶,舰长威武
迟来的支持!
papapapapapa
redirect uri is illegal(100010)
设置了以后出这个错误。。腾讯是不是又该什么了。
Missing required parameter "id" when instantiating "yii\authclient\AuthAction".
我参照你的方法以后报了这个错误。。谷歌了下。。还是没有发现是什么原因导致的。楼主可以帮忙看下。
不错,码上,过段时间也要做这个了
API的URL地址最好用CONST定义
我现在在frontend/widgets目录下创建了两个目录!接下来改怎么做呢?
接下来按照文档去做 https://github.com/yiisoft/yii2-authclient/tree/master/docs/guide-zh-CN
舰长,快速开始里面的Auth类哪来啊?
顶顶!正好用到,谢谢分享
哎呦!不错。。。
mark,以后可能会用到,顶起!
感恩站长!
支持。骑在奶妈身上跪舔舰长
不错,顶顶舰长