从Google Fusion Tables升级到Google API V.1 [英] Upgrading to Google Fusion Tables to Google API V.1

查看:58
本文介绍了从Google Fusion Tables升级到Google API V.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在升级我曾经为Google融合表制作的joomla组件.我下载了新的PHP API.但是我对如何开发网站以及使用oauth做些事情有一些疑问.

I am upgrading a joomla component i once made for google fusion tables. I downloaded the new php api. But I have some doubts about how I should develop my site and some things with oauth.

因此,我的网站读取了融合表,并允许我的(Joomla)用户更改,删除数据或向其中的任何表添加数据.所以我的问题是,我是否需要使用Web应用程序类型或服务帐户的客户端登录名.使用服务帐户似乎更合乎逻辑.如果是这样,我如何使用php和google php api框架进行连接.

So my site reads the fusion tables and allows my (Joomla) users to change, delete or add data to any of those tables. So my question is, do I need a client login that is a web application type or a service account. It seems more logically to use the service account. If so how do I connect using php and the google php api framework.

$clientlogin_curl = curl_init();
    curl_setopt($clientlogin_curl,CURLOPT_URL,'https://www.google.com/accounts/ClientLogin');
    curl_setopt($clientlogin_curl, CURLOPT_POST, true); 
    curl_setopt ($clientlogin_curl, CURLOPT_POSTFIELDS,
        "Email=".$username."&Passwd=".$password."&service=fusiontables&accountType=GOOGLE");
    curl_setopt($clientlogin_curl,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($clientlogin_curl,CURLOPT_RETURNTRANSFER,1);
    $token = curl_exec($clientlogin_curl);
    curl_close($clientlogin_curl);
    $token_array = explode("=", $token);
    $token = str_replace("\n", "", $token_array[3]);

上面是我以前的连接方式,但是现在我已经超过配额,因为它已被弃用.

Above it is how I used to connect, but now I get quota exceed, because it is deprecated.

我正在阅读 https://code. google.com/p/google-api-php-client/wiki/OAuth2 ,并且在服务帐户中未提及Fusion Tables.如果不是,我的重定向uri应该是什么

I was reading this, https://code.google.com/p/google-api-php-client/wiki/OAuth2, and in the part of service accounts Fusion Tables is not mentioned. If not what should my redirect uri should be

编辑

现在这是我的代码

jimport('gft-jdc.oauth-php.src.Google_Client');
jimport('gft-jdc.oauth-php.src.contrib.Google_PlusService');

const CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXX7pu.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'XXXXXXXXXXXXXXXXXXXpu@developer.gserviceaccount.com';
const KEY_FILE = '/home/jdc/workspace/xxxxxxxxxxxxxxxprivatekey.p12';


class ClientLogin {
    public static function getAuthToken($username, $password) {      
        $client = new Google_Client();
        $client->setApplicationName("API Project");
        var_dump(CLIENT_ID);
        var_dump(KEY_FILE);
        $client->setClientId(CLIENT_ID);

        // Set your cached access token. Remember to replace $_SESSION with a
        // real database or memcached.
        if (isset($_SESSION['token'])) {
            $client->setAccessToken($_SESSION['token']);
        }

        // Load the key in PKCS 12 format (you need to download this from the
        // Google API Console when the service account was created.
        $key = file_get_contents(KEY_FILE);

        $client->setAssertionCredentials(new Google_AssertionCredentials(
                SERVICE_ACCOUNT_NAME,
                array('https://www.googleapis.com/auth/fusiontables'),
                $key)
        );

        $client->authenticate();

        if ($client->getAccessToken()) {
            $_SESSION['token'] = $client->getAccessToken();
        }
        $token = $_SESSION['token'];

        var_dump($token);

        return $token;
    }
}

我收到此错误

解决方案, 我刚刚解决了最后一个问题,您不需要$ client-> authenticate()

Solution, I just solved this last problem, you dont need $client->authenticate()

这是一个解决方案如何获取使用Google API PHP客户端的OAuth2访问令牌?

if ($client->getAuth()->isAccessTokenExpired()) {
            $client->getAuth()->refreshTokenWithAssertion();
        }

推荐答案

  1. 使用服务帐户.服务帐户标识的是服务(例如网站),而不是个人.由于需要访问融合表的站点是您的站点,而不是您,因此访问服务帐户是您的最佳选择. (另一种选择是让您的用户一个一个地针对您的融合表进行身份验证.这将要求您的所有用户都拥有一个Google帐户,而您的网站必须为其授予权限.所有的麻烦,对吗?)

  1. Use a service account. Service account identifies a service(e.g: a website) and not a person. Since it is your site that needs to access the fusion tables - not you - service account is the way to go. (The other option would be to get your users to authenticate against your fusion tables one by one. This would require all of your users to have a google account and your site to give permission for them. Now we don't want to go through all the hassle, do we?)

使用 API控制台创建服务帐户.记下您的帐户名,客户ID.生成并下载您的.p12私钥文件.创建服务帐户后,与服务帐户的电子邮件地址(xxxx@developer.gserviceaccount.com)共享(也称为授予权限)您的融合表

Use the API Console to create a service account. Note your account name, client id. Generate and download your .p12 private key file. After you created a service account, share(a.k.a: give permissions) your fusion table with the service account's email address (xxxx@developer.gserviceaccount.com)

忘记卷曲.下载 Google API PHP客户端.在引擎盖下,它仍然会卷曲,但可以省去您的头痛.

Forget curl. Download the Google API PHP Client. Under the hood it will still use curl but saves you the headache.

在代码中包含api客户端并进行身份验证.

Include the api client in your code and do the authentication.

require_once '<path>/src/Google_Client.php';
require_once '<path>/src/contrib/Google_FusiontablesService.php';

$CLIENT_ID = 'xxx.apps.googleusercontent.com';
$SERVICE_ACCOUNT_NAME = 'xxx@developer.gserviceaccount.com';
$KEY_FILE = '<path to your .p12 file>'; //this should not be reached by any of your site's visitors

$client = new Google_Client();
$client->setApplicationName("whatever");
$client->setClientId($CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    $SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/fusiontables'),
    file_get_contents($KEY_FILE)
);  
$client->authenticate();


//do whatever you need to do with your fusion table on behalf of the user

这篇关于从Google Fusion Tables升级到Google API V.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆