2014-09-29 146 views
4

我正在嘗試在PHP中爲項目使用google-api-client。 我得到了一個 「權限被拒絕」 響應,而在此聲明:refreshTokenWithAssertion權限被拒絕

$client->getAuth()->refreshTokenWithAssertion(); 

Google_IO_Exception,消息:無法連接到74.125.193.84:權限被拒絕 文件:/home/www/blah.com/restful /libs/Google/IO/Curl.php Line:81 /home/www/blah.com/restful/libs/Google/IO/Abstract.php(125):Google_IO_Curl-> executeRequest(Object(Google_Http_Request))

#1 /home/www/blah.com/restful/libs/Google/Auth/OAuth2.php(326):Google_IO_Abstract-> makeRequest(Object(Google_Http_Request))

#2 /home/www/blah.com/restful/libs/Google/Auth/OAuth2.php(306):Google_Auth_OAuth2-> refreshTokenRequest(陣列)

#3 /家庭/網絡/等等。 COM /寧靜/ V2/index.php文件(122):Google_Auth_OAuth2-> refreshTokenWithAssertion()

我檢查我的所有憑證,他們看起來是正確的,可能是什麼問題呢?

感謝, 約翰

代碼:

$client_id = '1234blahblahblah.apps.googleusercontent.com'; //Client ID 
$service_account_name = '[email protected]'; //Email Address 
$key_file_location = 'blahblah-1234.p12'; //key.p12 

$client = new Google_Client(); 
$client->setApplicationName("test"); 
$service = new Google_Service_Calendar($client); 
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('https://www.googleapis.com/auth/calendar'), 
    $key 
    ); 

print_r($cred); 

$client->setAssertionCredentials($cred); 

$client->setClientId($client_id); 

if($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion(); //<<<<<failed here. 
} 
$_SESSION['service_token'] = $client->getAccessToken(); 
echo $_SESSION['service_token']; 
} 
+0

此代碼駐留在另一個restful .php文件中。我使用chrome的寧靜客戶端來測試.php,而後者又調用google api,這是測試它的正確方法嗎? – john 2014-10-07 06:04:50

+0

它需要與NTP同步。 請查看評論http://stackoverflow.com/questions/25376791/fatal-error-uncaught-exception-google-auth-exception-with-message-error-refr#comment39574276_25376949 – 2015-09-09 21:25:05

+0

方法Google_Client#loadServiceAccountJson($ jsonLocation,$範圍)可用於創建Google_Auth_AssertionCredentials對象。當前文檔中未提及此方法。它可以直接使用私鑰等所有必需的數據處理JSON文件。 [API身份驗證文檔](https://developers.google.com/api-client-library/php/auth/service-accounts) – 2015-11-21 12:38:17

回答

-3

service-account.php樣品中examples/目錄中的谷歌API客戶端庫的PHP的上Github.com:

if($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); // set credentials there 
} 
+0

這不會增加值 – John 2015-05-20 11:45:30

1

喜約翰我有同樣的問題,最後這對我有效:

行之前:

if($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion(); //<<<<<failed here. 
} 

我把一個嘗試捕捉並返回我,我有一個writtin權限問題:

try { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
} catch (Exception $e) { 
    var_dump($e->getMessage()); 
} 

我可以做兩兩件事:

1)圍棋到Google/src/Config.php並將第94行改爲:'directory'=> sys_get_temp_dir()。 '/ Google_Client'並改變目錄以保存緩存臨時文件

2)或者像我一樣,發一個echo sys_get_temp_dir();在嘗試捕獲並給予該目錄的chmod 777權限

此解決方案適用於我,我希望也適合您。無論如何做了一個try/catch等待異常消息

+0

感謝Alberto。但我試過這種方法,但它沒有奏效。從跟蹤中,錯誤發生在curl.php: $ response = curl_exec($ curl); if($ response === false){ throw new Google_IO_Exception(curl_error($ curl)); } – john 2014-10-08 04:08:33

+0

因此curl.php正在向google api服務器發送一個post請求,並且得到了「權限被拒絕」的響應,看起來像憑證不正確,但我檢查了他們沒有問題... – john 2014-10-08 06:05:56

+0

btw,echo sys_get_temp_dir()顯示'\ tmp',但我沒有在服務器上的這個文件夾。所以我創建了一個 – john 2014-10-08 06:07:16