-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefresh-token.php
More file actions
66 lines (54 loc) · 2.01 KB
/
refresh-token.php
File metadata and controls
66 lines (54 loc) · 2.01 KB
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
<?php
// Refresh ALM access token
$config = require(__DIR__ . '/config.php');
$tokenUrl = 'https://learningmanager.adobe.com/oauth/token';
$postData = [
'refresh_token' => $config['xapi']['admin']['refreshToken'],
'client_id' => $config['xapi']['admin']['clientId'],
'client_secret' => $config['xapi']['admin']['clientSecret'],
'grant_type' => 'refresh_token'
];
$ch = curl_init($tokenUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
$result = [
'httpCode' => $httpCode,
'response' => json_decode($response, true) ?: $response,
'error' => $error
];
if ($httpCode === 200 && isset($result['response']['access_token'])) {
// Update config with new access token
$newAccessToken = $result['response']['access_token'];
$newRefreshToken = $result['response']['refresh_token'] ?? $config['xapi']['admin']['refreshToken'];
// Read current config
$configContent = file_get_contents(__DIR__ . '/config.php');
// Update access token
$configContent = preg_replace(
"/'accessToken' => '[^']*'/",
"'accessToken' => '$newAccessToken'",
$configContent
);
// Update refresh token if new one provided
if (isset($result['response']['refresh_token'])) {
$configContent = preg_replace(
"/'refreshToken' => '[^']*'/",
"'refreshToken' => '$newRefreshToken'",
$configContent
);
}
// Save updated config
file_put_contents(__DIR__ . '/config.php', $configContent);
$result['message'] = 'Token refreshed successfully and config updated!';
}
header('Content-Type: application/json');
echo json_encode($result, JSON_PRETTY_PRINT);
?>