Weibo.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Overtrue\Socialite\Providers;
  3. use Overtrue\Socialite\Exceptions\InvalidTokenException;
  4. use Overtrue\Socialite\User;
  5. /**
  6. * @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 授权机制说明]
  7. */
  8. class Weibo extends Base
  9. {
  10. public const NAME = 'weibo';
  11. protected string $baseUrl = 'https://api.weibo.com';
  12. protected array $scopes = ['email'];
  13. protected function getAuthUrl(): string
  14. {
  15. return $this->buildAuthUrlFromBase($this->baseUrl.'/oauth2/authorize');
  16. }
  17. protected function getTokenUrl(): string
  18. {
  19. return $this->baseUrl.'/2/oauth2/access_token';
  20. }
  21. /**
  22. * @param string $code
  23. *
  24. * @return array
  25. */
  26. protected function getTokenFields(string $code): array
  27. {
  28. return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
  29. }
  30. /**
  31. * @param string $token
  32. *
  33. * @return array
  34. * @throws \GuzzleHttp\Exception\GuzzleException
  35. *
  36. * @throws \Overtrue\Socialite\Exceptions\InvalidTokenException
  37. */
  38. protected function getUserByToken(string $token): array
  39. {
  40. $uid = $this->getTokenPayload($token)['uid'] ?? null;
  41. if (empty($uid)) {
  42. throw new InvalidTokenException('Invalid token.', $token);
  43. }
  44. $response = $this->getHttpClient()->get($this->baseUrl.'/2/users/show.json', [
  45. 'query' => [
  46. 'uid' => $uid,
  47. 'access_token' => $token,
  48. ],
  49. 'headers' => [
  50. 'Accept' => 'application/json',
  51. ],
  52. ]);
  53. return \json_decode($response->getBody(), true) ?? [];
  54. }
  55. /**
  56. * @param string $token
  57. *
  58. * @return array
  59. * @throws \GuzzleHttp\Exception\GuzzleException
  60. * @throws \Overtrue\Socialite\Exceptions\InvalidTokenException
  61. */
  62. protected function getTokenPayload(string $token): array
  63. {
  64. $response = $this->getHttpClient()->post($this->baseUrl.'/oauth2/get_token_info', [
  65. 'query' => [
  66. 'access_token' => $token,
  67. ],
  68. 'headers' => [
  69. 'Accept' => 'application/json',
  70. ],
  71. ]);
  72. $response = \json_decode($response->getBody(), true) ?? [];
  73. if (empty($response['uid'])) {
  74. throw new InvalidTokenException(\sprintf('Invalid token %s', $token), $token);
  75. }
  76. return $response;
  77. }
  78. /**
  79. * @param array $user
  80. *
  81. * @return \Overtrue\Socialite\User
  82. */
  83. protected function mapUserToObject(array $user): User
  84. {
  85. return new User([
  86. 'id' => $user['id'] ?? null,
  87. 'nickname' => $user['screen_name'] ?? null,
  88. 'name' => $user['name'] ?? null,
  89. 'email' => $user['email'] ?? null,
  90. 'avatar' => $user['avatar_large'] ?? null,
  91. ]);
  92. }
  93. }