WeiboProvider.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\Socialite\Providers;
  11. use Overtrue\Socialite\AccessTokenInterface;
  12. use Overtrue\Socialite\ProviderInterface;
  13. use Overtrue\Socialite\User;
  14. /**
  15. * Class WeiboProvider.
  16. *
  17. * @see http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E [OAuth 2.0 授权机制说明]
  18. */
  19. class WeiboProvider extends AbstractProvider implements ProviderInterface
  20. {
  21. /**
  22. * The base url of Weibo API.
  23. *
  24. * @var string
  25. */
  26. protected $baseUrl = 'https://api.weibo.com';
  27. /**
  28. * The API version for the request.
  29. *
  30. * @var string
  31. */
  32. protected $version = '2';
  33. /**
  34. * The scopes being requested.
  35. *
  36. * @var array
  37. */
  38. protected $scopes = ['email'];
  39. /**
  40. * The uid of user authorized.
  41. *
  42. * @var int
  43. */
  44. protected $uid;
  45. /**
  46. * Get the authentication URL for the provider.
  47. *
  48. * @param string $state
  49. *
  50. * @return string
  51. */
  52. protected function getAuthUrl($state)
  53. {
  54. return $this->buildAuthUrlFromBase($this->baseUrl.'/oauth2/authorize', $state);
  55. }
  56. /**
  57. * Get the token URL for the provider.
  58. *
  59. * @return string
  60. */
  61. protected function getTokenUrl()
  62. {
  63. return $this->baseUrl.'/'.$this->version.'/oauth2/access_token';
  64. }
  65. /**
  66. * Get the Post fields for the token request.
  67. *
  68. * @param string $code
  69. *
  70. * @return array
  71. */
  72. protected function getTokenFields($code)
  73. {
  74. return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
  75. }
  76. /**
  77. * Get the raw user for the given access token.
  78. *
  79. * @param \Overtrue\Socialite\AccessTokenInterface $token
  80. *
  81. * @return array
  82. */
  83. protected function getUserByToken(AccessTokenInterface $token)
  84. {
  85. $response = $this->getHttpClient()->get($this->baseUrl.'/'.$this->version.'/users/show.json', [
  86. 'query' => [
  87. 'uid' => $token['uid'],
  88. 'access_token' => $token->getToken(),
  89. ],
  90. 'headers' => [
  91. 'Accept' => 'application/json',
  92. ],
  93. ]);
  94. return json_decode($response->getBody(), true);
  95. }
  96. /**
  97. * Map the raw user array to a Socialite User instance.
  98. *
  99. * @param array $user
  100. *
  101. * @return \Overtrue\Socialite\User
  102. */
  103. protected function mapUserToObject(array $user)
  104. {
  105. return new User([
  106. 'id' => $this->arrayItem($user, 'id'),
  107. 'nickname' => $this->arrayItem($user, 'screen_name'),
  108. 'name' => $this->arrayItem($user, 'name'),
  109. 'email' => $this->arrayItem($user, 'email'),
  110. 'avatar' => $this->arrayItem($user, 'avatar_large'),
  111. ]);
  112. }
  113. }