-
Notifications
You must be signed in to change notification settings - Fork 13
/
example.php
61 lines (51 loc) · 1.92 KB
/
example.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
61
<?php
spl_autoload_register(function ($class) {
require str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
});
// configuration of client credentials
$client = new OAuth2\Client(
'CLIENT_ID',
'CLIENT_SECRET',
'CALLBACK_URL');
// configuration of service
$configuration = new OAuth2\Service\Configuration(
'AUTHORIZE_ENDPOINT',
'ACCESS_TOKEN_ENDPOINT');
// storage class for access token, just implement OAuth2\DataStore interface for
// your own implementation
$dataStore = new OAuth2\DataStore\Session();
$scope = null;
$service = new OAuth2\Service($client, $configuration, $dataStore, $scope);
if (isset($_GET['action'])) {
switch ($_GET['action']) {
case 'authorize':
// redirects to authorize endpoint
$service->authorize();
break;
case 'requestApi':
// calls api endpoint with access token
echo $service->callApiEndpoint('API_ENDPOINT');
break;
}
}
if (isset($_GET['code'])) {
// retrieve access token from endpoint
$service->getAccessToken();
}
$token = $dataStore->retrieveAccessToken();
?>
<html>
<head>
</head>
<body>
Consumer Key: <input type="text" id="consumer-key" value="<?= $client->getClientKey() ?>" /><br />
Consumer Secret: <input type="text" id="consumer-secret" value="<?= $client->getClientSecret() ?>" /><br />
Access Token: <input type="text" id="access-token" value="<?= $token->getAccessToken() ?>" /><br />
Refresh Token: <input type="text" id="refresh-token" value="<?= $token->getRefreshToken() ?>" /><br />
LifeTime: <input type="text" id="lifetime" value="<?= $token->getLifeTime() ?>" /><br />
<br />
<a href="example.php?action=authorize" id="authorize">authorize</a><br />
<br />
<a href="example.php?action=requestApi" id="request-api">request API</a><br />
</body>
</html>