2015-06-09 71 views
1

我试图用Outh2验证谷歌的服务帐户,但不断收到此错误 -谷歌PHP API认证例外

异常 - 错误刷新的OAuth2令牌,消息:“{‘错误’: “ACCESS_DENIED “,”error_description“:”请求的客户端不是 授权。“ }”

我已按照https://developers.google.com/api-client-library/php/auth/service-accounts 每一个指令我也没有授权谷歌从管理控制台中的服务帐户,但仍然没有运气。任何人都可以请建议,如果在下面的代码中有任何错误 -

$client_email = [email protected]'; 
     $private_key = file_get_contents('private_key_file_location.p12'); 
     $scopes = array('https://spreadsheets.google.com/feeds'); 
     $user_to_impersonate = '[email protected]'; 
     $credentials = new Google_Auth_AssertionCredentials($client_email, $scopes, 
                  $private_key, 'notasecret', 
                  'http://oauth.net/grant_type/jwt/1.0/bearer', 
                  $user_to_impersonate); 

     $client = new Google_Client(); 

     $client->setApplicationName('Portal Assessment Module'); 
     $client->setAccessType('offline'); 

     $client->setAssertionCredentials($credentials); 
     if ($client->getAuth()->isAccessTokenExpired()) { 
      $client->getAuth()->refreshTokenWithAssertion($credentials); /* Exception 
is being triggered here */  
} 

谢谢。

回答

0

终于找到它了,不得不确保$ user_to_impersonate是一样的$ client_email,也该电子邮件地址具有对电子表格的编辑权限。

$user_to_impersonate = $client_email 
0

试试这个

require_once realpath(dirname(__FILE__).'/google-api-php-client/src/Google/autoload.php'); 

define('SCOPES', implode(' ', array(Google_Service_Appsactivity::DRIVE))); 

$credentials = new Google_Auth_AssertionCredentials(
    $client_email, 
    SCOPES, 
    $private_key 
); 

$client = new Google_Client(); 
$client->setAssertionCredentials($credentials); 

if ($client->getAuth()->isAccessTokenExpired()) 
{ 
    $client->getAuth()->refreshTokenWithAssertion(); 
} 

print_r($client->getAccessToken()); 
+0

感谢您的代码,但得到它使用我发布的答案工作 – Sandeep