-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathamo_authorization.php
More file actions
68 lines (46 loc) · 2.42 KB
/
amo_authorization.php
File metadata and controls
68 lines (46 loc) · 2.42 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
<?php
/*
* Copyright (c) 2021. AMO | Корпоративный мессенджер.
* Это приложение - демо для документации по API.
* Вы можете использовать данный код в своих проектах без сохранения этого копирайта.
*
* @Author Mike Eremin <meremin@team.amocrm.com>
*/
use League\OAuth2\Client\Provider\GenericProvider;
require_once 'vendor/autoload.php';
$appURL = "https://{$_SERVER['HTTP_HOST']}";
$clientId = $_ENV['CLIENT_ID'] ?? null;
$clientSecret = $_ENV['CLIENT_SECRET'] ?? null;
$appName = str_replace( '.herokuapp.com', '', $_SERVER['SERVER_NAME']);
$settingsURL = "https://dashboard.heroku.com/apps/{$appName}/settings";
if (!$clientId || !$clientSecret) {
echo "Пожалуйста, настройте переменные окружения CLIENT_ID и CLIENT_SECRET в настройках приложения на сайте heroku.com. {$settingsURL}";
}
$provider = new GenericProvider([
'clientId' => $clientId, // The client ID assigned to you by the provider
'clientSecret' => $clientSecret, // The client password assigned to you by the provider
'redirectUri' => "{$appURL}/amo_authorization.php",
'urlAuthorize' => 'https://id.amo.tm/access',
'urlAccessToken' => 'https://id.amo.tm/oauth2/access_token',
'urlResourceOwnerDetails' => null
]);
if (!isset($_GET['code'])) {
exit('Invalid code');
} else {
try {
// Try to get an access token using the authorization code grant.
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// We have an access token, which we may use in authenticated
// requests against the service provider's API.
echo 'Access Token: ' . $accessToken->getToken() . "<br>";
echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
echo 'Expired in: ' . $accessToken->getExpires() . "<br>";
echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";
echo '<script>setTimeout(function(){window.close()}, 15 * 1000);</script>';
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token or user details.
exit($e->getMessage());
}
}