Google Api客户端PHP - 创建Fusion表

时间:2014-05-09 08:49:01

标签: php google-api google-fusion-tables google-api-php-client

使用最新版本的Google Api Client for PHP,很难找到有效的示例。文档也非常渺茫..我尝试了很多,无法弄清楚如何登录/验证,所以我可以创建/插入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::
            }*/
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为这可能会对你有所帮助。这是来自zend的php-client的官方文档 http://framework.zend.com/manual/1.11/en/zend.gdata.photos.html

但我建议使用oAuth 2.0