1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
namespace Chamilo\PluginBundle\Zoom\API; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
class ServerToServerOAuthClient extends Client |
10
|
|
|
{ |
11
|
|
|
use Api2RequestTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
public $token; |
17
|
|
|
|
18
|
|
|
public function __construct(string $accountId, string $clientId, string $clientSecret) |
19
|
|
|
{ |
20
|
|
|
try { |
21
|
|
|
$this->token = $this->requireAccessToken($accountId, $clientId, $clientSecret); |
22
|
|
|
} catch (Exception $e) { |
23
|
|
|
error_log('Zoom: Can\'t require access token: '.$e->getMessage()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
self::register($this); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function requireAccessToken(string $accountId, string $clientId, string $clientSecret) |
30
|
|
|
{ |
31
|
|
|
$options = [ |
32
|
|
|
CURLOPT_CUSTOMREQUEST => 'POST', |
33
|
|
|
CURLOPT_ENCODING => '', |
34
|
|
|
CURLOPT_HTTPHEADER => [ |
35
|
|
|
'Authorization: Basic '.base64_encode("$clientId:$clientSecret"), |
36
|
|
|
'Content-Type: application/x-www-form-urlencoded', |
37
|
|
|
'Host: zoom.us', |
38
|
|
|
], |
39
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
40
|
|
|
CURLOPT_MAXREDIRS => 10, |
41
|
|
|
CURLOPT_RETURNTRANSFER => true, |
42
|
|
|
CURLOPT_TIMEOUT => 30, |
43
|
|
|
CURLOPT_POST => true, |
44
|
|
|
CURLOPT_POSTFIELDS => http_build_query([ |
45
|
|
|
'grant_type' => 'account_credentials', |
46
|
|
|
'account_id' => $accountId, |
47
|
|
|
]), |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
$url = 'https://zoom.us/oauth/token'; |
51
|
|
|
|
52
|
|
|
$curl = curl_init($url); |
53
|
|
|
|
54
|
|
|
if (false === $curl) { |
55
|
|
|
throw new Exception("curl_init returned false"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
curl_setopt_array($curl, $options); |
59
|
|
|
$responseBody = curl_exec($curl); |
60
|
|
|
$responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); |
61
|
|
|
$curlError = curl_error($curl); |
62
|
|
|
curl_close($curl); |
63
|
|
|
|
64
|
|
|
if ($curlError) { |
65
|
|
|
throw new Exception("cURL Error: $curlError"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (false === $responseBody || !is_string($responseBody)) { |
69
|
|
|
throw new Exception('cURL Error'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (empty($responseCode) || $responseCode < 200 || $responseCode >= 300) { |
73
|
|
|
throw new Exception($responseBody, $responseCode); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$jsonResponseBody = json_decode($responseBody, true); |
77
|
|
|
|
78
|
|
|
if (false === $jsonResponseBody) { |
79
|
|
|
throw new Exception('Could not generate JSON responso body'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $jsonResponseBody['access_token']; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|