WeiboAuth.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace common\modules\user\clients;
  3. use yii\authclient\OAuth2;
  4. class WeiboAuth extends OAuth2
  5. {
  6. public $authUrl = 'https://api.weibo.com/oauth2/authorize';
  7. public $tokenUrl = 'https://api.weibo.com/oauth2/access_token';
  8. public $apiBaseUrl = 'https://api.weibo.com';
  9. /**
  10. *
  11. * @return array
  12. * @see http://open.weibo.com/wiki/Oauth2/get_token_info
  13. * @see http://open.weibo.com/wiki/2/users/show
  14. */
  15. protected function initUserAttributes()
  16. {
  17. $attributes = $this->api('oauth2/get_token_info', 'POST');
  18. $result = $this->api("2/users/show.json", 'GET', [
  19. 'uid' => $attributes['uid']
  20. ]);
  21. $result['login'] = $result['name'];
  22. $result['email'] = $result['name'] . '@weibo.com';
  23. $result['avatar'] = $result['avatar_large'];
  24. return $result;
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. public function getEmail()
  30. {
  31. return isset($this->getUserAttributes()['email']) ? $this->getUserAttributes()['email'] : null;
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function getUsername()
  37. {
  38. return isset($this->getUserAttributes()['name']) ? $this->getUserAttributes()['name'] : null;
  39. }
  40. protected function defaultName()
  41. {
  42. return 'weibo';
  43. }
  44. protected function defaultTitle()
  45. {
  46. return '微博';
  47. }
  48. }