2015-06-09 40 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