2019.7.27 制作qq登录功能网上找了诸多教程发现有一些因为官方插件更新出来的新bug。。遂改之。
官方authclient
"yiisoft/yii2-authclient": "^2.2"
新建common/components/Qq.php
<?php namespace common\components; use yii\authclient\OAuth2; use yii\base\InvalidParamException; use yii\httpclient\Request; use yii\web\HttpException; use Yii; /** * QQ allows authentication via QQ OAuth. * * In order to use QQ OAuth you must register your application at <http://connect.qq.com/>. * * Example application configuration: * * ~~~ * 'components' => [ * 'authClientCollection' => [ * 'class' => 'yii\authclient\Collection', * 'clients' => [ * 'qq' => [ * 'class' => 'yujiandong\authclient\Qq', * 'clientId' => 'qq_appid', * 'clientSecret' => 'qq_appkey', * ], * ], * ] * ... * ] * ~~~ * * @see http://connect.qq.com/ * @see http://wiki.connect.qq.com/ * */ class Qq extends OAuth2 { /** * @inheritdoc */ public $authUrl = 'https://graph.qq.com/oauth2.0/authorize'; /** * @inheritdoc */ public $tokenUrl = 'https://graph.qq.com/oauth2.0/token'; /** * @inheritdoc */ public $apiBaseUrl = 'https://graph.qq.com'; /** * @inheritdoc */ public function init() { parent::init(); if ($this->scope === null) { $this->scope = implode(' ', [ 'get_user_info', ]); } } /** * @inheritdoc */ public function buildAuthUrl(array $params = []) { //$authState = $this->generateAuthState(); //$this->setState('authState', $authState); //$params['state'] = $authState; return parent::buildAuthUrl($params); } /** * @inheritdoc */ public function fetchAccessToken($authCode, array $params = []) { /*$authState = $this->getState('authState'); if (!isset($_REQUEST['state']) || empty($authState) || strcmp($_REQUEST['state'], $authState) !== 0) { throw new HttpException(400, 'Invalid auth state parameter2.'); } else { //$this->removeState('authState'); }*/ return parent::fetchAccessToken($authCode, $params); } /** * @inheritdoc */ protected function defaultNormalizeUserAttributeMap() { return [ 'username' => 'nickname', ]; } /** * @inheritdoc */ protected function initUserAttributes() { $user = $this->api('oauth2.0/me', 'GET'); if ( isset($user['error']) ) { throw new HttpException(400, $user['error']. ':'. $user['error_description']); } $userAttributes = $this->api( "user/get_user_info", 'GET', [ 'oauth_consumer_key' => $user['client_id'], 'openid' => $user['openid'], ] ); $userAttributes['id'] = $user['openid']; return $userAttributes; } public function createApiRequest() { $request=parent::createApiRequest(); $request->on(Request::EVENT_AFTER_SEND, [$this, 'processResponse']); return $request; } /** * @inheritdoc */ protected function processResponse($event) { $rawResponse=$event->response->getContent(); if ( strpos($rawResponse, "callback(") === 0) { $count = 0; $jsonData = preg_replace('/^callback\(\s*(\\{.*\\})\s*\);$/is', '\1', $rawResponse, 1, $count); if ($count === 1) { $rawResponse = $jsonData; $contentType = 'json'; } } $event->response->setContent($rawResponse); return $event; } /** * Generates the auth state value. * @return string auth state value. */ protected function generateAuthState() { return sha1(uniqid(get_class($this), true)); } /** * @inheritdoc */ protected function defaultReturnUrl() { $params = $_GET; unset($params['code']); unset($params['state']); $params[0] = Yii::$app->controller->getRoute(); return Yii::$app->getUrlManager()->createAbsoluteUrl($params); } /** * @inheritdoc */ protected function defaultName() { return 'qq'; } /** * @inheritdoc */ protected function defaultTitle() { return 'QQ'; } /** * @inheritdoc */ protected function defaultViewOptions() { return [ 'popupWidth' => 800, 'popupHeight' => 500, ]; } }
main.php 组件中加入 也就是 comonents接点
'authClientCollection' => [ 'class' => 'yii\authclient\Collection', 'clients' => [ 'qq' => [ 'class' => 'common\components\Qq', 'clientId' => '', 'clientSecret' => '', ], ], ]
siteController.php
public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], 'auth' => [ 'class' => 'yii\authclient\AuthAction', 'successCallback' => [$this, 'successCallback'], ], ]; } public function successCallback($client) { $attributes = $client->getUserAttributes(); // 用户的信息在$attributes中,以下是您根据您的实际情况增加的代码 // 如果您同时有QQ互联登录,新浪微博等,可以通过 $client->id 来区别。 var_dump($attributes);die; }
访问/site/auth?authclient=qq