forked from frkosurf/php-voot-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
56 lines (47 loc) · 1.72 KB
/
index.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
<?php
require_once 'vendor/autoload.php';
require_once 'config.php';
use fkooman\OAuth\Client\ClientConfig;
use fkooman\OAuth\Client\SessionStorage;
use fkooman\OAuth\Client\Api;
use fkooman\OAuth\Client\Context;
use fkooman\OAuth\Client\Scope;
use fkooman\Guzzle\Plugin\BearerAuth\BearerAuth;
use fkooman\Guzzle\Plugin\BearerAuth\Exception\BearerErrorResponseException;
use Guzzle\Http\Client;
$clientConfig = new ClientConfig($config['client']);
$tokenStorage = new SessionStorage();
$httpClient = new Client();
$api = new Api("php-voot-client", $clientConfig, $tokenStorage, $httpClient);
$context = new Context(
new Scope($config['scope'])
);
$accessToken = $api->getAccessToken($context);
if (false === $accessToken) {
/* no valid access token available, go to authorization server */
header("HTTP/1.1 302 Found");
header("Location: " . $api->getAuthorizeUri($context));
exit;
}
try {
$client = new Client();
$bearerAuth = new BearerAuth($accessToken->getAccessToken());
$client->addSubscriber($bearerAuth);
$response = $client->get($config['api_uri'])->send();
header("Content-Type: application/json");
echo $response->getBody();
} catch (BearerErrorResponseException $e) {
if ("invalid_token" === $e->getBearerReason()) {
// the token we used was invalid, possibly revoked, we throw it away
$api->deleteAccessToken($context);
$api->deleteRefreshToken($context);
/* no valid access token available, go to authorization server */
header("HTTP/1.1 302 Found");
header("Location: " . $api->getAuthorizeUri($context));
exit;
}
throw $e;
} catch (Exception $e) {
die(sprintf('ERROR: %s', $e->getMessage()));
}