本文整理汇总了PHP中Google_Client::useApplicationDefaultCredentials方法的典型用法代码示例。如果您正苦于以下问题:PHP Google_Client::useApplicationDefaultCredentials方法的具体用法?PHP Google_Client::useApplicationDefaultCredentials怎么用?PHP Google_Client::useApplicationDefaultCredentials使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Google_Client
的用法示例。
在下文中一共展示了Google_Client::useApplicationDefaultCredentials方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testFileCache
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);
}
示例2: auth
/**
* 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();
}
示例3: getClient
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;
}
示例4: __construct
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);
}
示例5: createAuthorizedClient
function createAuthorizedClient()
{
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Bigquery::BIGQUERY);
$service = new Google_Service_Bigquery($client);
return $service;
}
示例6: __construct
/**
* 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;
}
示例7: Application
*/
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();
示例8: get_token
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'];
}