【发布时间】:2014-05-09 08:49:00
【问题描述】:
使用最新版本的适用于 PHP 的 Google Api 客户端,很难找到有效的示例。文档也很薄。我尝试了很多,但不知道如何登录/验证,所以我可以创建/插入 Fusion Tables。 我已经尝试使用 oAuth 和服务令牌(使用 p12 文件)。
我能够从 Fusion Table 中检索数据并将其记录到 FireBug(使用 FirePHP)。但这只需要开发者 ID,不需要身份验证。
如果有人有一个可行的例子,我会非常感激!
我的试验:
class GoogleClass {
protected $firephp;
public function init() {
session_start();
require_once 'Google/Client.php';
require_once 'Google/Auth/AssertionCredentials.php';
#require_once 'Google/Auth/OAuth2.php';
require_once 'Google/Service/Fusiontables.php';
#require_once 'Google/Service/Urlshortener.php';
#require_once 'Google/Service/Oauth2.php';
/*
Service Auth
$client_id = 'client_id';
$service_account_name = 'service_account_name';
$key_file_location = 'key_file_location';
$client = new Google_Client();
$client->setApplicationName('FusionTableConnect');
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('http://www.googleapis.com/auth/fusiontables'),
$key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
try {
$client->getAuth()->refreshTokenWithAssertion($cred);
} catch (Exception $e) {
$this->firephp->error($e);
}
}
$_SESSION['service_token'] = $client->getAccessToken();
*/
/*
Execute Query to get Table results
*/
$client = new Google_Client();
$client->setDeveloperKey('developerKeyServer'); // Server Application API key - Query
$tableId = 'tableId';
$ft = new Google_Service_Fusiontables($client);
try {
$result = $ft->query->sql("SELECT gm_code FROM $tableId");
$this->firephp->log($result, 'Array');
} catch (Exception $e) {
$this->firephp->error($e);
}
/* ------------------------------------------------------------------------------ */
/*
Google Client setup
$client = new Google_Client();
#$client->setApplicationName('TestApp');
$client->setClientId('clientId'); // Web application Client ID
$client->setClientSecret('clientSecret'); // Web application Secret key
#$client->addScope('https://www.googleapis.com/auth/fusiontables'); // Fusion Tables scope
$client->setRedirectUri('redirectUri');
#$client->setDeveloperKey('developerKeyServer'); // Server Application API key - Query
#$client->setDeveloperKey('developerKeyBrowser'); // Browser Application API key
*/
/*
oAuth2 Identification
$service = new Google_Service_Urlshortener($client);
#$client->addScope(Google_Service_Urlshortener::URLSHORTENER);
$client->setScopes("https://www.googleapis.com/auth/plus.login");
$authUrl = $client->createAuthUrl();
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
}
*/
/*
Fusion Tables
$service = new Google_Service_Fusiontables($client);
try {
$result = $service->query->sql("SELECT gm_code FROM tableId");
$this->firephp->log($result, 'Array');
} catch (Exception $e) {
$this->firephp->error($e);
}
$column = new Google_Service_Fusiontables_Column();
$column->setName('foo');
$column->setType('STRING');
$table = new Google_Service_Fusiontables_Table();
$table->setName('bar');
$table->setColumns(array($column));
try {
$service->table->insert($table);
} catch(Exception $e) {
$this->firephp->error($e);
}*/
}
public function firePHPInit($auto) {
require_once('FirePHPCore/FirePHP.class.php');
ob_start();
$this->firephp = FirePHP::getInstance(true);
if ($auto) {
$this->firephp->registerErrorHandler(
$throwErrorExceptions=false);
$this->firephp->registerExceptionHandler();
$this->firephp->registerAssertionHandler(
$convertAssertionErrorsToExceptions=true,
$throwAssertionExceptions=false);
/*try {
throw new Exception('Test Exception');
} catch(Exception $e) {
$this->firephp->error($e); // or FB::
}*/
}
}
}
【问题讨论】:
-
没有人提供如何使用 PHP Google 客户端的工作示例?
标签: php google-api google-fusion-tables google-api-php-client