我正在尝试在PHP中使用google-api-client进行项目。 在本声明中我收到了“许可被拒绝”的回复:
$client->getAuth()->refreshTokenWithAssertion();
Google_IO_Exception,消息:无法连接到74.125.193.84:权限被拒绝 文件:/home/www/blah.com/restful/libs/Google/IO/Curl.php 行: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(Array)
#3 /home/www/blah.com/restful/v2/index.php(122):Google_Auth_OAuth2-> refreshTokenWithAssertion()
我检查了所有凭据,看起来没错,可能是什么问题?
谢谢, 约翰
代码:
$client_id = '1234blahblahblah.apps.googleusercontent.com'; //Client ID
$service_account_name = '1234blahblah@developer.gserviceaccount.com'; //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 :(得分:1)
嗨,约翰我也有同样的问题,最后这对我有用:
在行之前:
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion(); //<<<<<failed here.
}
我试了一下try,这让我觉得我有写权限问题:
try {
$client->getAuth()->refreshTokenWithAssertion($cred);
} catch (Exception $e) {
var_dump($e->getMessage());
}
我可以做两件事:
1)转到Google / src / Config.php并更改第94行:'directory'=&gt; sys_get_temp_dir()。 '/ Google_Client'并更改目录以保存缓存临时文件
2)或者像我一样,做了一个echo sys_get_temp_dir();在try catch之前,给那个目录一个chmod 777权限
这个解决方案对我有用,我希望也适合你。无论如何做了一个try / catch等待异常消息
答案 1 :(得分:-3)
在Github.com上查看适用于PHP的Google API客户端库的service-account.php目录中的examples/示例:
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred); // set credentials there
}