首页 文章

Google API:“invalid_grant”,但代码未更改

提问于
浏览
2

我对Google API的身份验证存在以下问题:

PHP致命错误:未捕获异常'Google_Auth_Exception',并显示错误'刷新OAuth2令牌,消息:'/“/ bot / bot / vendor / google / apiclient / src / Google中的'{”错误“:”invalid_grant“}' /Auth/OAuth2.php:328

但是,问题非常奇怪,因为相同的代码在我的本地PC上工作并停止在服务器上工作 . 是否可能,它与服务器时间有关?我的系统时钟同步,但服务器向后约1分钟 . 说真的,我没有想法为什么它不再起作用了,虽然我根本没有改变代码 . 它以前曾经工作过,但突然出现了这个问题 .

我该怎么办?

<?php
$client = new Google_Client();
$client->setApplicationName('My App');
$client->setClientId($clientId);
$client->setClassConfig(
    'Google_Cache_File',
    ['directory' => __DIR__.'/res/tmp']
);

$service = new Google_Service_Calendar($client);

$cred = new Google_Auth_AssertionCredentials(
    $serviceAccountName,
    // https://www.googleapis.com/auth/calendar
    [Google_Service_Calendar::CALENDAR],
    $keyFile
);
$client->setAssertionCredentials($cred);

var_dump($service->calendarList->listCalendarList());

1 回答

  • 2

    解决方案是同步服务器时间 . 服务器时间与原子钟不同只有40秒,但在向我的托管服务提供商发送票证后,我设法让它运行起来 . 问题已经消失 . 但是,如果有人能解释为什么OAuth在确切的日期和时间如此严格,我将不胜感激 .

相关问题