|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CloudyCity\TencentMarketingSDK; |
|
4
|
|
|
|
|
5
|
|
|
use CloudyCity\TencentMarketingSDK\Kernel\Exceptions\AuthException; |
|
6
|
|
|
use CloudyCity\TencentMarketingSDK\Kernel\Traits\HasHttpRequests; |
|
7
|
|
|
|
|
8
|
|
|
class Auth |
|
9
|
|
|
{ |
|
10
|
|
|
use HasHttpRequests; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $clientId; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $clientSecret; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $url = 'https://api.e.qq.com/oauth/token'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Auth constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param $clientId |
|
31
|
|
|
* @param $clientSecret |
|
32
|
|
|
* @param $responseType |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($clientId, $clientSecret, $responseType = 'array') |
|
35
|
|
|
{ |
|
36
|
|
|
$this->setClientId($clientId); |
|
37
|
|
|
$this->setClientSecret($clientSecret); |
|
38
|
|
|
$this->setResponseType($responseType); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getClientId() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->clientId; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $clientId |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setClientId($clientId) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->clientId = $clientId; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getClientSecret() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->clientSecret; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $clientSecret |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setClientSecret($clientSecret) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->clientSecret = $clientSecret; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $authCode |
|
75
|
|
|
* @param $redirectUri |
|
76
|
|
|
* |
|
77
|
|
|
* @throws AuthException |
|
78
|
|
|
* @throws Kernel\Exceptions\Exception |
|
79
|
|
|
* @throws Kernel\Exceptions\InvalidArgumentException |
|
80
|
|
|
* |
|
81
|
|
|
* @return array|Kernel\Http\Response|\Doctrine\Common\Collections\ArrayCollection|object|\Psr\Http\Message\ResponseInterface |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getTokens($authCode, $redirectUri) |
|
84
|
|
|
{ |
|
85
|
|
|
$params = [ |
|
86
|
|
|
'client_id' => $this->getClientId(), |
|
87
|
|
|
'client_secret' => $this->getClientSecret(), |
|
88
|
|
|
'grant_type' => 'authorization_code', |
|
89
|
|
|
'authorization_code' => $authCode, |
|
90
|
|
|
'redirect_uri' => $redirectUri, |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
return $this->get($this->url, $params); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $refreshToken |
|
98
|
|
|
* |
|
99
|
|
|
* @throws AuthException |
|
100
|
|
|
* @throws Kernel\Exceptions\Exception |
|
101
|
|
|
* @throws Kernel\Exceptions\InvalidArgumentException |
|
102
|
|
|
* |
|
103
|
|
|
* @return array|Kernel\Http\Response|\Doctrine\Common\Collections\ArrayCollection|object|\Psr\Http\Message\ResponseInterface |
|
104
|
|
|
*/ |
|
105
|
|
|
public function refreshTokens($refreshToken) |
|
106
|
|
|
{ |
|
107
|
|
|
$params = [ |
|
108
|
|
|
'client_id' => $this->getClientId(), |
|
109
|
|
|
'client_secret' => $this->getClientSecret(), |
|
110
|
|
|
'grant_type' => 'refresh_token', |
|
111
|
|
|
'refresh_token' => $refreshToken, |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
|
|
return $this->get($this->url, $params); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $url |
|
119
|
|
|
* @param array $params |
|
120
|
|
|
* |
|
121
|
|
|
* @throws AuthException |
|
122
|
|
|
* @throws Kernel\Exceptions\Exception |
|
123
|
|
|
* @throws Kernel\Exceptions\InvalidArgumentException |
|
124
|
|
|
* |
|
125
|
|
|
* @return array|Kernel\Http\Response|\Doctrine\Common\Collections\ArrayCollection|object|\Psr\Http\Message\ResponseInterface |
|
126
|
|
|
*/ |
|
127
|
|
|
private function get($url, array $params) |
|
128
|
|
|
{ |
|
129
|
|
|
$response = $this->request($url, 'GET', [ |
|
130
|
|
|
'query' => $params, |
|
131
|
|
|
]); |
|
132
|
|
|
|
|
133
|
|
|
$result = $this->castResponseToType($response); |
|
134
|
|
|
$formatted = $this->castResponseToType($response, $this->getResponseType()); |
|
135
|
|
|
|
|
136
|
|
|
if (!isset($result['code']) || $result['code'] != 0) { |
|
137
|
|
|
$message = isset($result['message']) ? $result['message'] : ''; |
|
138
|
|
|
$code = isset($result['code']) ? $result['code'] : 0; |
|
139
|
|
|
|
|
140
|
|
|
throw new AuthException($message, $response, $formatted, $code); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $formatted; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|