当尝试刷新令牌时- Google Oauth - PHP


Invlid_grant When trying to refresh token - Google Oauth - PHP

刚刚开始使用google admin SDK,试图检索特定用户的信息。我不断得到相同的错误

[29-May-2015 14:41:18 Africa/Tunis] PHP Fatal error:  Uncaught
exception 'Google_Auth_Exception' with message 'Error refreshing the
OAuth2 token, message: '{   "error" : "invalid_grant"
在我的研究中,我发现这主要是一个同步问题(服务器时间)。但我的网络应用托管在第三方服务器上。检查了我的服务帐户凭据

Ps:不知道是否有关系,但是服务器的时区是GMT+1

<?php
require 'src/Google/autoload.php';
session_start();
$timestamp = $_SERVER['REQUEST_TIME'];
echo gmdate("Y-m-d'TH:i:s'Z", $timestamp);
$service_account_name = "xx";
$key_file_location = "yyy.p12";
$client = new Google_Client();
$client->setApplicationName("Members");
$directory = new Google_Service_Directory($client);
if (isset($_SESSION['service_token']) && $_SESSION['service_token']) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
  // Replace this with the email address from the client.
  $service_account_name,
  // Replace this with the scopes you are requesting.
  array('https://www.googleapis.com/admin/directory/v1/users'),
  $key,
  'notasecret'
);
$cred->sub = "admin-email";
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

$email = "personto lookfor-email";
$r = $dir->users->get($email);
if($r) {
     echo "Name: ".$r->name->fullName."<br/>";
     echo "Suspended?: ".(($r->suspended === true) ? 'Yes' : 'No')."<br/>";
     echo "Org/Unit/Path: ".$r->orgUnitPath."<br/>";
} else {
     echo "User does not exist: $email<br/>";
     // if the user doesn't exist, it's safe to create the new user
}

"

出现"Invalid grant"错误的原因可能是刷新令牌不工作。当刷新令牌的数量超过限制时,旧的令牌将失效。如果应用程序试图使用无效的刷新令牌,则返回invalid_grant错误响应。这里是更多文档的链接