public function testFileCache()
 {
     $this->onlyPhp55AndAbove();
     $this->checkServiceAccountCredentials();
     $client = new Google_Client();
     $client->useApplicationDefaultCredentials();
     $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']);
     // filecache with new cache dir
     $cache = $this->getCache(sys_get_temp_dir() . '/cloud-samples-tests-php-cache-test/');
     $client->setCache($cache);
     $token1 = null;
     $client->setTokenCallback(function ($cacheKey, $accessToken) use($cache, &$token1) {
         $token1 = $accessToken;
         $cacheItem = $cache->getItem($cacheKey);
         // expire the item
         $cacheItem->expiresAt(new DateTime('now -1 second'));
         $cache->save($cacheItem);
         $cacheItem2 = $cache->getItem($cacheKey);
     });
     /* Refresh token when expired */
     if ($client->isAccessTokenExpired()) {
         $client->refreshTokenWithAssertion();
     }
     /* Make a service call */
     $service = new Google_Service_Drive($client);
     $files = $service->files->listFiles();
     $this->assertInstanceOf('Google_Service_Drive_FileList', $files);
     sleep(1);
     // make sure the token expires
     $client = new Google_Client();
     $client->useApplicationDefaultCredentials();
     $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']);
     $client->setCache($cache);
     $token2 = null;
     $client->setTokenCallback(function ($cacheKey, $accessToken) use(&$token2) {
         $token2 = $accessToken;
     });
     /* Make another service call */
     $service = new Google_Service_Drive($client);
     $files = $service->files->listFiles();
     $this->assertInstanceOf('Google_Service_Drive_FileList', $files);
     $this->assertNotEquals($token1, $token2);
 }
Example #2
1
 private function get_token()
 {
     $client = new Google_Client();
     if ($credentials_file = $this->checkServiceAccountCredentialsFile()) {
         $client->setAuthConfig($credentials_file);
     } elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
         $client->useApplicationDefaultCredentials();
     } else {
         echo missingServiceAccountDetailsWarning();
         exit;
     }
     $client->setApplicationName("IARD Tables");
     $client->setScopes(array('https://spreadsheets.google.com/feeds'));
     $token = $client->fetchAccessTokenWithAssertion();
     return $token['access_token'];
 }
Example #3
0
 /**
  * Setup correct auth method based on type.
  *
  * @param $userEmail
  * @return void
  */
 protected function auth($userEmail = '')
 {
     // see (and use) if user has set Credentials
     if ($this->useAssertCredentials($userEmail)) {
         return;
     }
     // fallback to compute engine or app engine
     $this->client->useApplicationDefaultCredentials();
 }
Example #4
0
function getClient()
{
    // Authenticate your API Client
    $client = new Google_Client();
    //$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);
    $client->addScope(Google_Service_Storage::DEVSTORAGE_READ_WRITE);
    // see ~/sandbox/zouk-event-calendar/vendor/google/apiclient/src/Google/Service/Storage.php
    $client->setAccessType("offline");
    $client->useApplicationDefaultCredentials();
    // no need to acquire special credentials
    $token = $client->getAccessToken();
    if (!$token) {
        // this is always the case, and same access token is aquired in the fetch call below (can be printed)
        //syslog(LOG_DEBUG, "girish: access token not present");
        $token = $client->fetchAccessTokenWithAssertion();
        $client->setAccessToken($token);
        //syslog(LOG_DEBUG, $token['access_token']);
    }
    // token acquried above is always expired. and even if you run fetchAccess...Refreshtoken() it still stays expired
    //if ($client->isAccessTokenExpired()) {
    //    //syslog(LOG_DEBUG, "girish: access token expired");
    //    $client->fetchAccessTokenWithRefreshToken($token);
    //}
    //if ($client->isAccessTokenExpired()) {
    //    syslog(LOG_DEBUG, "girish: access token still expired!"); // no idea how this works
    //}
    return $client;
}
 public function __construct($datastoreDatasetId)
 {
     $this->datasetId = $datastoreDatasetId;
     $client = new \Google_Client();
     $client->setScopes([Google_Service_Datastore::CLOUD_PLATFORM, Google_Service_Datastore::DATASTORE, Google_Service_Datastore::USERINFO_EMAIL]);
     $client->useApplicationDefaultCredentials();
     $this->datastore = new \Google_Service_Datastore($client);
 }
Example #6
0
function createAuthorizedClient()
{
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope(Google_Service_Bigquery::BIGQUERY);
    $service = new Google_Service_Bigquery($client);
    return $service;
}
 /**
  * CloudStorage constructor.
  *
  * @param string         $bucketName The cloud storage bucket name
  * @param \Google_Client $client     When null, a new Google_client is created
  *                                   that uses default application credentials.
  */
 public function __construct($bucketName, \Google_Client $client = null)
 {
     if (!$client) {
         $client = new \Google_Client();
         $client->useApplicationDefaultCredentials();
         $client->setApplicationName('php bookshelf');
         $client->setScopes(\Google_Service_Storage::DEVSTORAGE_READ_WRITE);
     }
     $this->service = new \Google_Service_Storage($client);
     $this->bucketName = $bucketName;
 }
Example #8
0
 */
use Google\Cloud\Samples\Datastore\DatastoreHelper;
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\UrlGeneratorServiceProvider;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
// create the silex application
$app = new Application();
$app->register(new TwigServiceProvider());
$app->register(new UrlGeneratorServiceProvider());
$app['twig.path'] = [__DIR__ . '/../templates'];
// create the google api client
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Datastore::DATASTORE);
$client->addScope(Google_Service_Datastore::USERINFO_EMAIL);
// add the api client and project id to our silex app
$app['google_client'] = $client;
$app['project_id'] = getenv('GCP_PROJECT_ID');
$app->get('/', function () use($app) {
    /** @var Google_Client $client */
    $client = $app['google_client'];
    /** @var Twig_Environment $twig */
    $twig = $app['twig'];
    // run a simple query to retrieve the comments
    // - last 20 items ordered by created DESC
    $projectId = $app['project_id'];
    $datastore = new Google_Service_Datastore($client);
    $util = new DatastoreHelper();