我正在尝试在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