-
Notifications
You must be signed in to change notification settings - Fork 108
/
get_token.php
60 lines (51 loc) · 1.74 KB
/
get_token.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
60
<?php
include_once __DIR__ . '/bootstrap.php';
session_start();
if (isset($_GET['referer'])) {
$apiClient->setAccountBaseDomain($_GET['referer']);
}
if (!isset($_GET['code'])) {
$state = bin2hex(random_bytes(16));
$_SESSION['oauth2state'] = $state;
if (isset($_GET['button'])) {
echo $apiClient->getOAuthClient()->getOAuthButton(
[
'title' => 'Установить интеграцию',
'compact' => true,
'class_name' => 'className',
'color' => 'default',
'error_callback' => 'handleOauthError',
'state' => $state,
]
);
die;
} else {
$authorizationUrl = $apiClient->getOAuthClient()->getAuthorizeUrl([
'state' => $state,
'mode' => 'post_message',
]);
header('Location: ' . $authorizationUrl);
die;
}
} elseif (!isset($_GET['from_widget']) && (empty($_GET['state']) || empty($_SESSION['oauth2state']) || ($_GET['state'] !== $_SESSION['oauth2state']))) {
unset($_SESSION['oauth2state']);
exit('Invalid state');
}
/**
* Ловим обратный код
*/
try {
$accessToken = $apiClient->getOAuthClient()->getAccessTokenByCode($_GET['code']);
if (!$accessToken->hasExpired()) {
saveToken([
'accessToken' => $accessToken->getToken(),
'refreshToken' => $accessToken->getRefreshToken(),
'expires' => $accessToken->getExpires(),
'baseDomain' => $apiClient->getAccountBaseDomain(),
]);
}
} catch (Exception $e) {
die((string)$e);
}
$ownerDetails = $apiClient->getOAuthClient()->getResourceOwner($accessToken);
printf('Hello, %s!', $ownerDetails->getName());