/
Provider.php
executable file
·142 lines (120 loc) · 3.29 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace SocialiteProviders\QQ;
use GuzzleHttp\RequestOptions;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'QQ';
/**
* @var string
*/
private $openId;
/**
* User unionid.
*
* @var string
*/
protected $unionId;
/**
* get token(openid) with unionid.
*
* @var bool
*/
protected $withUnionId = false;
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['get_user_info'];
/**
* {@inheritdoc}.
*
* @see \Laravel\Socialite\Two\AbstractProvider::getAuthUrl()
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://graph.qq.com/oauth2.0/authorize', $state);
}
/**
* {@inheritdoc}.
*
* @see \Laravel\Socialite\Two\AbstractProvider::getTokenUrl()
*/
protected function getTokenUrl()
{
return 'https://graph.qq.com/oauth2.0/token';
}
/**
* @param bool $value
* @return self
*/
public function withUnionId($value = true)
{
$this->withUnionId = $value;
return $this;
}
/**
* {@inheritdoc}.
*
* @see \Laravel\Socialite\Two\AbstractProvider::getUserByToken()
*/
protected function getUserByToken($token)
{
$queryParams = [
'fmt' => 'json',
'access_token' => $token,
];
if ($this->withUnionId) {
$queryParams['unionid'] = 1;
}
$response = $this->getHttpClient()->get('https://graph.qq.com/oauth2.0/me', [
RequestOptions::QUERY => $queryParams,
]);
$me = json_decode((string) $response->getBody(), true);
$this->openId = $me['openid'];
$this->unionId = $me['unionid'] ?? '';
$response = $this->getHttpClient()->get('https://graph.qq.com/user/get_user_info', [
RequestOptions::QUERY => [
'access_token' => $token,
'openid' => $this->openId,
'oauth_consumer_key' => $this->clientId,
],
]);
return json_decode((string) $response->getBody(), true);
}
/**
* {@inheritdoc}.
*
* @see \Laravel\Socialite\Two\AbstractProvider::mapUserToObject()
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $this->openId, 'unionid' => $this->unionId, 'nickname' => $user['nickname'],
'name' => null, 'email' => null, 'avatar' => $user['figureurl_qq_2'],
]);
}
/**
* {@inheritdoc}.
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'fmt' => 'json',
]);
}
/**
* {@inheritdoc}.
*
* @see \Laravel\Socialite\Two\AbstractProvider::getAccessToken()
*/
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->get($this->getTokenUrl(), [
RequestOptions::QUERY => $this->getTokenFields($code),
]);
return json_decode((string) $response->getBody(), true);
}
}