22
33namespace UDX \Zoom \Http ;
44
5- use Firebase \JWT \JWT ;
65use GuzzleHttp \Client ;
76use GuzzleHttp \Exception \ClientException ;
87use GuzzleHttp \Psr7 \Response ;
@@ -12,12 +11,17 @@ class Request {
1211 /**
1312 * @var
1413 */
15- protected $ apiKey ;
14+ protected $ accountId ;
1615
1716 /**
1817 * @var
1918 */
20- protected $ apiSecret ;
19+ protected $ clientId ;
20+
21+ /**
22+ * @var
23+ */
24+ protected $ clientSecret ;
2125
2226 /**
2327 * @var Client
@@ -29,15 +33,24 @@ class Request {
2933 */
3034 public $ apiPoint = 'https://api.zoom.us/v2/ ' ;
3135
36+ /**
37+ * @var string
38+ */
39+ public $ oauthPoint = 'https://zoom.us/oauth/token?grant_type=account_credentials&account_id= ' ;
40+
41+
3242 /**
3343 * Request constructor.
34- * @param $apiKey
35- * @param $apiSecret
44+ * @param $accountId
45+ * @param $clientId
46+ * @param $clientSecret
3647 */
37- public function __construct ( $ apiKey , $ apiSecret ) {
38- $ this ->apiKey = $ apiKey ;
48+ public function __construct ( $ accountId , $ clientId , $ clientSecret ) {
49+ $ this ->accountId = $ accountId ;
3950
40- $ this ->apiSecret = $ apiSecret ;
51+ $ this ->clientId = $ clientId ;
52+
53+ $ this ->clientSecret = $ clientSecret ;
4154
4255 $ this ->client = new Client ();
4356 }
@@ -49,24 +62,28 @@ public function __construct( $apiKey, $apiSecret ) {
4962 */
5063 protected function headers (): array {
5164 return [
52- 'Authorization ' => 'Bearer ' . $ this ->generateJWT (),
65+ 'Authorization ' => 'Bearer ' . $ this ->generateOAuthToken (),
5366 'Content-Type ' => 'application/json ' ,
5467 'Accept ' => 'application/json ' ,
5568 ];
5669 }
5770
5871 /**
59- * Generate J W T
72+ * Generate OAuth token
6073 *
6174 * @return string
6275 */
63- protected function generateJWT () {
64- $ token = [
65- 'iss ' => $ this ->apiKey ,
66- 'exp ' => time () + 60 ,
67- ];
76+ protected function generateOAuthToken () {
77+ try {
78+ $ response = $ this ->client ->request ('POST ' , $ this ->oauthPoint . $ this ->accountId , [
79+ 'auth ' => [ $ this ->clientId , $ this ->clientSecret ]
80+ ]);
81+ $ result = $ this ->result ($ response );
82+ return isset ($ result ['access_token ' ]) ? $ result ['access_token ' ] : '' ;
6883
69- return JWT ::encode ($ token , $ this ->apiSecret , 'HS256 ' );
84+ } catch (ClientException $ e ) {
85+ return (array )json_decode ($ e ->getResponse ()->getBody ()->getContents ());
86+ }
7087 }
7188
7289
0 commit comments