2013-10-22 61 views
1

我升級了一次爲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; 
    } 
} 

我得到這個錯誤

enter image description here

解決方案, 我只是解決了這個最後的問題,你不需要$ client-> authenticate()

這裏是一個解決方案How to get OAuth2 access token with Google API PHP client?

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

回答

2
  1. 使用服務帳戶。服務帳戶標識服務(例如:網站)而不是個人。由於它是您的網站需要訪問融合表 - 不是你 - 服務帳戶是要走的路。 (另一種選擇是讓用戶逐一驗證你的聚合表,這將要求你的所有用戶都有一個谷歌帳戶和你的網站給他們許可,現在我們不想通過所有的麻煩,我們呢?)

  2. 使用API Console創建一個服務帳戶。請注意您的帳戶名稱,客戶端ID。生成並下載您的.p12私鑰文件。您創建了一個服務帳戶後,份額(a.k.a:授予權限)的融合表與服務帳戶的電子郵件地址([email protected]

  3. 忘記捲曲。下載Google API PHP Client。在引擎蓋下面,它仍然會使用捲曲,但可以爲您節省頭痛。

  4. 在您的代碼中包含api客戶端並進行身份驗證。

    require_once '<path>/src/Google_Client.php'; 
    require_once '<path>/src/contrib/Google_FusiontablesService.php'; 
    
    $CLIENT_ID = 'xxx.apps.googleusercontent.com'; 
    $SERVICE_ACCOUNT_NAME = '[email protected]'; 
    $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 
    
+0

我得到錯誤:INVALID_REQUEST 缺少必需的參數:範圍 什麼是融合表範圍,我在看的例子,但融合的表沒有任何即時的例子。謝謝 –

+0

哪一行你會得到這個錯誤信息?它不應該是一個問題,範圍是在setAssertionCredentials()成員函數的第二個參數中定義的。 – sanya

+0

對不起,我的意思是Google_AssertionCredentials構造函數的第二個參數。 – sanya

相關問題