rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

index.php (2212B)


      1 <?php
      2 
      3 use fkooman\OAuth\Client\Api;
      4 use fkooman\OAuth\Client\Context;
      5 use fkooman\OAuth\Client\GoogleClientConfig;
      6 use fkooman\OAuth\Client\SessionStorage;
      7 use fkooman\OAuth\Client\PdoStorage;
      8 use Guzzle\Http\Client;
      9 use fkooman\Guzzle\Plugin\BearerAuth\BearerAuth;
     10 use fkooman\Guzzle\Plugin\BearerAuth\Exception\BearerErrorResponseException;
     11 
     12 require_once 'vendor/autoload.php';
     13 
     14 try {
     15     /* OAuth client configuration */
     16     $clientConfig = new GoogleClientConfig(json_decode(file_get_contents("client_secrets.json"), true));
     17 
     18     //$db = new PDO(sprintf("sqlite:%s/data/client.sqlite", __DIR__));
     19     //$tokenStorage = new PdoStorage($db);
     20     $tokenStorage = new SessionStorage();
     21 
     22     $api = new Api("php-drive-client", $clientConfig, $tokenStorage, new Client());
     23     $context = new Context("john.doe@example.org", array("https://www.googleapis.com/auth/drive.readonly"));
     24 
     25     /* the protected endpoint uri */
     26     $apiUri = "https://www.googleapis.com/drive/v2/files";
     27 
     28     /* get the access token */
     29     $accessToken = $api->getAccessToken($context);
     30     if (false === $accessToken) {
     31         /* no valid access token available just yet, go to authorization server */
     32         header("HTTP/1.1 302 Found");
     33         header("Location: ".$api->getAuthorizeUri($context));
     34         exit;
     35     }
     36 
     37     /* we have an access token */
     38     try {
     39         $client = new Client();
     40         $bearerAuth = new BearerAuth($accessToken->getAccessToken());
     41         $client->addSubscriber($bearerAuth);
     42         $response = $client->get($apiUri)->send();
     43 
     44         header("Content-Type: application/json");
     45         echo $response->getBody();
     46     } catch (BearerErrorResponseException $e) {
     47         if ("invalid_token" === $e->getBearerReason()) {
     48             /* no valid access token available just yet, go to authorization server */
     49             $api->deleteAccessToken($context);
     50 
     51             // Google does not support refresh tokens...
     52             // $api->deleteRefreshToken($context);
     53 
     54             header("HTTP/1.1 302 Found");
     55             header("Location: ".$api->getAuthorizeUri($context));
     56             exit;
     57         }
     58         throw $e;
     59     }
     60 } catch (Exception $e) {
     61     echo sprintf("ERROR: %s", $e->getMessage());
     62 }