Yii2 使用 QQ 和 Weibo 第三方登录源码
2016-12-26 21:30
405 查看
我们社区在
所以本次直接在
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 '微博登录'; } }
相关文章推荐
- 使用OAuth2.0协议的github、QQ、weibo第三方登录接入总结
- 第三方网站使用qq登录
- [iOS常见问题] 关于使用QQ做第三方登录的问题!
- 使用QQ第三方登录时,手机应用和网站应用对同一个QQ号,获取到的openid不一样
- 解决!百度云管家使用QQ第三方登录时提示“由于网络原因无法载入页面 请点击刷新后重试”
- 使用ShareSDK完成第三方(QQ、微信、微博)登录和分享
- 关于使用QQ、新浪微博、腾讯微博等第三方登录网站的开发过程(一)
- 利用webqq协议使用python登录qq发消息源码参考
- cordova 实现第三方登录及分享,qq,微信,微博,插件的使用和改动
- thinkPHP3.2.2使用thinkSDK实现第三方登录(以QQ登录)为例。
- 关于Android Studio第三方登录(使用QQ登录)代码
- 关于使用QQ、新浪微博、腾讯微博等第三方登录网站的开发过程(二)
- 第三方登录 QQ WEIBO
- 第三方插件使用以及qq,新浪微博登录
- 使用Sharesdk实现第三方平台登录(qq,新浪微博)
- 第三方登录接入-qq,weibo-java
- 关于使用QQ、新浪微博、腾讯微博等第三方登录网站的开发过程(一)
- ShareSDK [iOS常见问题] 关于使用QQ做第三方登录的问题!
- 使用ShareSDK完成第三方(QQ、微信、微博)登录和分享
- 利用webqq协议使用python登录qq发消息源码参考