2014-09-29 102 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