refreshTokenWithAssertion权限被拒绝 [英] refreshTokenWithAssertion Permission Denied

查看:102
本文介绍了refreshTokenWithAssertion权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PHP中将google-api-client用于一个项目. 在此声明中,我得到了权限被拒绝"响应:

I am trying to use google-api-client in PHP for a project. I got a "permission denied" response while at this statement:

$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))

Google_IO_Exception, Message:Failed to connect to 74.125.193.84: Permission denied File: /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))

#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)

#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()

#3 /home/www/blah.com/restful/v2/index.php(122): Google_Auth_OAuth2->refreshTokenWithAssertion()

我检查了所有凭据,它们看起来正确,这可能是什么问题?

I checked all my credentials and they look correct, what could be the problem?

谢谢, 约翰

代码:

$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'];
}

推荐答案

约翰,我遇到了同样的问题,最后对我有用:

Hi John I´ve the same problem and finally this works for me:

在各行之前:

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

我试了一下,然后返回我有写权限问题:

I put a try catch and that returns me that I had a writtin permissions problem:

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'并更改目录以保存缓存临时文件

1) Go to Google/src/Config.php and change line 94: 'directory' => sys_get_temp_dir() . '/Google_Client'and change the directory to save cache temp files

2)或像我一样,做了一个回声sys_get_temp_dir();在尝试捕获之前,并对该目录授予chmod 777权限

2) or like me, made a echo sys_get_temp_dir(); before the try catch and give a chmod 777 permission to that dir

此解决方案对我有用,我也希望对您有用.无论如何都要尝试/捕获以等待异常消息

This solution works for me, I hope also for you. Anyway made an try/catch waiting for the exception message

这篇关于refreshTokenWithAssertion权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆