Installation 安装

通过 composer 安装该应用

  1. php composer.phar require --prefer-dist yiisoft/yii2-authclient "*"

Quick start 快速开始

更改Yii2的配置文件config/main.php,在components中增加如下内容

  1. 'components' => [
  2. 'authClientCollection' => [
  3. 'class' => 'yii\authclient\Collection',
  4. 'clients' => [
  5. 'google' => [
  6. 'class' => 'yii\authclient\clients\GoogleOpenId'
  7. ],
  8. 'facebook' => [
  9. 'class' => 'yii\authclient\clients\Facebook',
  10. 'clientId' => 'facebook_client_id',
  11. 'clientSecret' => 'facebook_client_secret',
  12. ],
  13. ],
  14. ]
  15. ...
  16. ]

更改入口文件,一般是app/controllers/SiteController.php,在function actions增加代码,同时增加回调函数successCallback,大致如下

  1. class SiteController extends Controller
  2. {
  3. public function actions()
  4. {
  5. return [
  6. 'auth' => [
  7. 'class' => 'yii\authclient\AuthAction',
  8. 'successCallback' => [$this, 'successCallback'],
  9. ],
  10. ]
  11. }
  12.  
  13. public function successCallback($client)
  14. {
  15. $attributes = $client->getUserAttributes();
  16. // user login or signup comes here
  17. }
  18. }

在登录的Views中,增加如下代码

  1. <?= yii\authclient\widgets\AuthChoice::widget([
  2. 'baseAuthUrl' => ['site/auth']
  3. ]) ?>

以上是官方的说明文档,下面我们来接入QQ互联

增加QQ登录的组件 我这里是放在 common/components/QqOAuth.php 中,源代码如下

  1. <?php
  2. namespace common\components;
  3.  
  4. use yii\authclient\OAuth2;
  5. use yii\base\Exception;
  6. use yii\helpers\Json;
  7.  
  8. /**
  9. *
  10. * ~~~
  11. * 'components' => [
  12. * 'authClientCollection' => [
  13. * 'class' => 'yii\authclient\Collection',
  14. * 'clients' => [
  15. * 'qq' => [
  16. * 'class' => 'common\components\QqOAuth',
  17. * 'clientId' => 'qq_client_id',
  18. * 'clientSecret' => 'qq_client_secret',
  19. * ],
  20. * ],
  21. * ]
  22. * ...
  23. * ]
  24. * ~~~
  25. *
  26. * @see http://connect.qq/
  27. *
  28. * @author easypao <admin@easypao>
  29. * @since 2.0
  30. */
  31. class QqOAuth extends OAuth2
  32. {
  33. public $authUrl = 'https://graph.qq/oauth2.0/authorize';
  34. public $tokenUrl = 'https://graph.qq/oauth2.0/token';
  35. public $apiBaseUrl = 'https://graph.qq';
  36.  
  37. public function init()
  38. {
  39. parent::init();
  40. if ($this->scope === null) {
  41. $this->scope = implode(',', [
  42. 'get_user_info',
  43. ]);
  44. }
  45. }
  46.  
  47. protected function initUserAttributes()
  48. {
  49. $openid = $this->api('oauth2.0/me', 'GET');
  50. $qquser = $this->api("user/get_user_info", 'GET', ['oauth_consumer_key'=>$openid['client_id'], 'openid'=>$openid['openid']]);
  51. $qquser['openid']=$openid['openid'];
  52. return $qquser;
  53. }
  54.  
  55. protected function defaultName()
  56. {
  57. return 'qq';
  58. }
  59.  
  60. protected function defaultTitle()
  61. {
  62. return 'Qq';
  63. }
  64.  
  65.  
  66. /**
  67. * 该扩展初始的处理方法似乎QQ互联不能用,应此改写了方法
  68. * @see \yii\authclient\BaseOAuth::processResponse()
  69. */
  70. protected function processResponse($rawResponse, $contentType = self::CONTENT_TYPE_AUTO)
  71. {
  72. if (empty($rawResponse)) {
  73. return [];
  74. }
  75. switch ($contentType) {
  76. case self::CONTENT_TYPE_AUTO: {
  77. $contentType = $this->determineContentTypeByRaw($rawResponse);
  78. if ($contentType == self::CONTENT_TYPE_AUTO) {
  79. //以下代码是特别针对QQ互联登录的,也是与原方法不一样的地方
  80. if(strpos($rawResponse, "callback") !== false){
  81. $lpos = strpos($rawResponse, "(");
  82. $rpos = strrpos($rawResponse, ")");
  83. $rawResponse = substr($rawResponse, $lpos + 1, $rpos - $lpos -1);
  84. $response = $this->processResponse($rawResponse, self::CONTENT_TYPE_JSON);
  85. break;
  86. }
  87. //代码添加结束
  88. throw new Exception('Unable to determine response content type automatically.');
  89. }
  90. $response = $this->processResponse($rawResponse, $contentType);
  91. break;
  92. }
  93. case self::CONTENT_TYPE_JSON: {
  94. $response = Json::decode($rawResponse, true);
  95. if (isset($response['error'])) {
  96. throw new Exception('Response error: ' . $response['error']);
  97. }
  98. break;
  99. }
  100. case self::CONTENT_TYPE_URLENCODED: {
  101. $response = [];
  102. parse_str($rawResponse, $response);
  103. break;
  104. }
  105. case self::CONTENT_TYPE_XML: {
  106. $response = $this->convertXmlToArray($rawResponse);
  107. break;
  108. }
  109. default: {
  110. throw new Exception('Unknown response type "' . $contentType . '".');
  111. }
  112. }
  113.  
  114. return $response;
  115. }
  116.  
  117.  
  118. }

更改 config/main.php 文件,在components中增加,大致如下

  1. 'components' => [
  2. 'authClientCollection' => [
  3. 'class' => 'yii\authclient\Collection',
  4. 'clients' => [
  5. 'qq' => [
  6. 'class'=>'common\components\QqOAuth',
  7. 'clientId'=>'your_qq_clientid',
  8. 'clientSecret'=>'your_qq_secret'
  9. ],
  10. ],
  11. ]
  12. ]

SiteController.php 就按官方那样子

  1. public function successCallback($client)
  2. {
  3. $attributes = $client->getUserAttributes();
  4. // 用户的信息在$attributes中,以下是您根据您的实际情况增加的代码
  5. // 如果您同时有QQ互联登录,新浪微博等,可以通过 $client->id 来区别。
  6.  
  7. }

最后在登录的视图文件中 增加QQ登录链接

  1. <a href="/site/auth?authclient=qq">使用QQ快速登录</a>