我正在嘗試在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'];
}
此代碼駐留在另一個restful .php文件中。我使用chrome的寧靜客戶端來測試.php,而後者又調用google api,這是測試它的正確方法嗎? – john 2014-10-07 06:04:50
它需要與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
方法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