-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_actions.php
59 lines (52 loc) · 1.61 KB
/
token_actions.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
<?php
use League\OAuth2\Client\Token\AccessToken;
define('TOKEN_FILE', DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'token_info.json');
/**
* @param array $accessToken
*/
function saveToken($accessToken)
{
if (
isset($accessToken)
&& isset($accessToken['accessToken'])
&& isset($accessToken['refreshToken'])
&& isset($accessToken['expires'])
&& isset($accessToken['baseDomain'])
) {
$data = [
'accessToken' => $accessToken['accessToken'],
'expires' => $accessToken['expires'],
'refreshToken' => $accessToken['refreshToken'],
'baseDomain' => $accessToken['baseDomain'],
];
file_put_contents(TOKEN_FILE, json_encode($data));
} else {
exit('Invalid access token ' . var_export($accessToken, true));
}
}
/**
* @return AccessToken
*/
function getToken()
{
if (!file_exists(TOKEN_FILE)) {
exit('Access token file not found');
}
$accessToken = json_decode(file_get_contents(TOKEN_FILE), true);
if (
isset($accessToken)
&& isset($accessToken['accessToken'])
&& isset($accessToken['refreshToken'])
&& isset($accessToken['expires'])
&& isset($accessToken['baseDomain'])
) {
return new AccessToken([
'access_token' => $accessToken['accessToken'],
'refresh_token' => $accessToken['refreshToken'],
'expires' => $accessToken['expires'],
'baseDomain' => $accessToken['baseDomain'],
]);
} else {
exit('Invalid access token ' . var_export($accessToken, true));
}
}