2012-11-22 156 views
1

我试图让OAuth访问令牌将一些数据导入融合表。我正在尝试使用Google API PHP客户端。我创建用于该目的的服务帐户,和我使用的代码,大多是从serviceAccount例如:如何使用Google API PHP客户端获取OAuth2访问令牌?

function access_token() 
{ 
    $client = new Google_Client(); 
    $client->setAuthClass ('Google_OAuth2'); 
    //^Don't know if this line is required, 
    //^but it fails just as well without it. 
    $client->setApplicationName ('Mysite.dom.ain'); 
    $client->setAssertionCredentials (new Google_AssertionCredentials 
     ( '[email protected]', 
      array ('https://www.googleapis.com/auth/fusiontables'), 
      file_get_contents ('path/to/my/privatekey.p12'))); 
    $client->setClientId ('NUMBERS-LETTERS-DASHES.apps.googleusercontent.com'); 
    $client->authenticate(); 
    //^Also fails equally good with and without this line. 
    return $client->getAccessToken(); 
} 

一点点调试输出显示$client->authenticate()回报true,但$client->getAcessToken()回报null。没有例外被抛出。我感觉我正在做一些根本性错误的事情。如果是这样,请原谅我的愚蠢,并指向正确的方向。

回答

0

我觉得你所做的一切是正确的,现在你有两个选择离开:

  • 用你的$client做一个服务调用的东西像
$service = new Google_FusiontablesService($client); 
    $selectQuery = "select * from 1AwxQ46kfmPoYoq38e5CopJOWkCo_9GUU_ucD6zI"; 
    $service->query->sql($selectQuery) 
  • 或拨打内部功能refreshTokenWithAssertion()为了得到您的令牌:
$client::$auth->refreshTokenWithAssertion(); 
    $token = $client->getAccessToken(); //this should work now 

对于bothcases我在我的GitHub Repo中有例子。

+0

如何在java代码中获取访问令牌?任何关于它的帮助 – Vartika

5

您不需要authenticate()调用,但需要调用refreshTokenWithAssertion()来刷新基础访问令牌。如果您使用客户端库进行签名请求,如果基础访问令牌已过期,它将懒惰地为您进行此调用。

刷新access_token的API请求很昂贵,并且配额较低,因此您需要缓存access_token。

// Set your client id, service account name, and the path to your private key. 
// For more information about obtaining these keys, visit: 
// https://developers.google.com/console/help/#service_accounts 
const CLIENT_ID = 'INSERT_YOUR_CLIENT_ID'; 
const SERVICE_ACCOUNT_NAME = 'INSERT_YOUR_SERVICE_ACCOUNT_NAME'; 

// Make sure you keep your key.p12 file in a secure location, and isn't 
// readable by others. 
const KEY_FILE = '/super/secret/path/to/key.p12'; 

$client = new Google_Client(); 
$client->setApplicationName("Google FusionTable Sample"); 

// Set your cached access token. Remember to store the token in a real database instead of $_SESSION. 
session_start(); 
if (isset($_SESSION['token'])) { 
$client->setAccessToken($_SESSION['token']); 
} 

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

$client->setClientId(CLIENT_ID); 

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

// Get the json encoded access token. 
$token = $client->getAccessToken(); 
+0

嗨Chirag,我使用OAuth2.0和他们的PHP库与GA集成。如果我有本地存储的访问令牌以及用于脱机使用的刷新令牌。我现在看到我的访问令牌已过期,我仍然可以访问该API。我知道它使用刷新令牌发出请求。在我的if语句中检查是否isAccessTokenExpired我已经把'$ client-> refreshToken($ refreshtoken); $ token = $ client-> getAccessToken();'这是正确的调用,以使'$ refreshtoken'成为我存储的刷新令牌。然后我将新的访问令牌存储在我的数据库中,是否正确? – Anagio

相关问题