所以我有一段代码片段在星期五工作正常,但现在却抛出异常:
// 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
错误,但是没有人在一天内停止工作。关于可能导致这种情况的任何想法都会很棒。
答案 0 :(得分:2)
如果它之前正在工作但您没有进行其他更改,请检查您的系统时钟;这是授予失败的最常见原因。如果时钟超前或太远,授权将失败。