index.php (1867B)
1 <?php 2 3 require_once 'vendor/autoload.php'; 4 5 $apiUri = "http://localhost/oauth/php-oauth/api.php/authorizations/"; 6 7 $clientConfig = new fkooman\OAuth\Client\ClientConfig( 8 array( 9 "authorize_endpoint" => "http://localhost/oauth/php-oauth/authorize.php", 10 "client_id" => "php-oauth-client-example", 11 "client_secret" => "f00b4r", 12 "token_endpoint" => "http://localhost/oauth/php-oauth/token.php", 13 ) 14 ); 15 16 $tokenStorage = new fkooman\OAuth\Client\SessionStorage(); 17 $httpClient = new Guzzle\Http\Client(); 18 $api = new fkooman\OAuth\Client\Api("foo", $clientConfig, $tokenStorage, $httpClient); 19 20 $context = new fkooman\OAuth\Client\Context("john.doe@example.org", array("authorizations")); 21 22 $accessToken = $api->getAccessToken($context); 23 if (false === $accessToken) { 24 /* no valid access token available, go to authorization server */ 25 header("HTTP/1.1 302 Found"); 26 header("Location: ".$api->getAuthorizeUri($context)); 27 exit; 28 } 29 30 try { 31 $client = new Guzzle\Http\Client(); 32 $bearerAuth = new fkooman\Guzzle\Plugin\BearerAuth\BearerAuth($accessToken->getAccessToken()); 33 $client->addSubscriber($bearerAuth); 34 $response = $client->get($apiUri)->send(); 35 header("Content-Type: application/json"); 36 echo $response->getBody(); 37 } catch (fkooman\Guzzle\Plugin\BearerAuth\Exception\BearerErrorResponseException $e) { 38 if ("invalid_token" === $e->getBearerReason()) { 39 // the token we used was invalid, possibly revoked, we throw it away 40 $api->deleteAccessToken($context); 41 $api->deleteRefreshToken($context); 42 /* no valid access token available, go to authorization server */ 43 header("HTTP/1.1 302 Found"); 44 header("Location: ".$api->getAuthorizeUri($context)); 45 exit; 46 } 47 throw $e; 48 } catch (Exception $e) { 49 die(sprintf('ERROR: %s', $e->getMessage())); 50 }