Google_Auth_Exception: invalid_grant



所以我有一段代码在周五运行良好,但现在抛出了一个异常:

// Load the Google API PHP Client Library.
include_once __DIR__.'/vendor/autoload.php';
// Use the developers console and replace the values with your
// service account email, and relative location of your key file.
$service_account_email = 'thing@developer.gserviceaccount.com';
$key_file_location = 'thing.p12';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("HelloAnalytics");
$analytics = new Google_Service_Analytics($client);
// Read the generated client_secrets.p12 key.
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_email,
    array(Google_Service_Analytics::ANALYTICS_READONLY),
    $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}

其中,我在此处插入了实际值的thing。也就是说,这个例外是

Fatal error: Uncaught exception 'Google_Auth_Exception' 
with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' 
in repo/vendor/google/apiclient/src/Google/Auth/OAuth2.php:364 
Stack trace: 
#0 repo/vendor/google/apiclient/src/Google/Auth/OAuth2.php(315): Google_Auth_OAuth2->refreshTokenRequest(Array) 
#1 repo/test.php(25): Google_Auth_OAuth2->refreshTokenWithAssertion(Object(Google_Auth_AssertionCredentials)) 
#2 {main} thrown in repo/vendor/google/apiclient/src/Google/Auth/OAuth2.php on line 364

我发现很多人都有这个invalid_grant错误,但没有一个人有一天停止工作。任何可能导致这种情况的想法都会很棒。

如果它以前工作,并且您没有进行其他更改,请检查系统时钟;这是拨款失败的最常见原因。如果时间提前或落后太多,拨款就会失败。

相关内容

  • 没有找到相关文章

最新更新