我升級了一次爲google fusion表製作的joomla組件。我下載了新的php api。但我對如何開發自己的網站以及使用oauth進行一些操作有一些疑問。將Google Fusion Tables升級到Google API V.1
所以我的網站讀取融合表,並允許我的(Joomla)用戶更改,刪除或添加數據到這些表中的任何一個。所以我的問題是,我是否需要一個Web應用程序類型或服務帳戶的客戶端登錄。使用服務帳戶似乎更符合邏輯。如果是這樣,我怎麼連接使用PHP和谷歌PHP API的框架。
$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]);
以上是我如何連接,但現在我得到的配額超過,因爲它已被棄用。
我在讀這篇文章,https://code.google.com/p/google-api-php-client/wiki/OAuth2,在服務帳號的部分沒有提到Fusion Tables。如果沒有什麼應該我重定向URI應
編輯
這是我的代碼現在
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 = '[email protected]';
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()
這裏是一個解決方案How to get OAuth2 access token with Google API PHP client?
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
我得到錯誤:INVALID_REQUEST 缺少必需的參數:範圍 什麼是融合表範圍,我在看的例子,但融合的表沒有任何即時的例子。謝謝 –
哪一行你會得到這個錯誤信息?它不應該是一個問題,範圍是在setAssertionCredentials()成員函數的第二個參數中定義的。 – sanya
對不起,我的意思是Google_AssertionCredentials構造函數的第二個參數。 – sanya