Google API:"invalid_grant",但代码未更改 [英] Google API: "invalid_grant", but the code haven't changed

查看:287
本文介绍了Google API:"invalid_grant",但代码未更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following problem with authentication to Google API:

PHP致命错误:消息为刷新OAuth2令牌时出错,消息为:"{ 错误":"invalid_grant" }'',位于/home/bot/bot/vendor/google/apiclient/src/Google/Auth/OAuth2.php:328

PHP Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in /home/bot/bot/vendor/google/apiclient/src/Google/Auth/OAuth2.php:328

但是,问题很奇怪,因为相同的代码在我的本地PC上可以工作,并且在服务器上停止工作.是否有可能与服务器时间有关?我的系统时钟已同步,但是服务器后退了约1分钟.认真地讲,尽管我根本没有更改代码,但我还是想到了为什么它不再起作用的想法.它曾经曾经工作过,但是突然出现了这个问题.

However, the issue is very strange, because same code works on my local PC and stops working on the server. Is it possible, that it has something to do with server time? I have my system clock synchronized, but server is about ~1 minute backwards. Seriously, I ran out of ideas why it doesn't work anymore though I haven't changed the code at all. It used to work before, but all of the sudden this problem appeared.

我该怎么办?

<?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());

推荐答案

该解决方案正在同步服务器时间.服务器时间与原子钟仅相差40秒,但是在向我的托管服务提供商发送票证后,我设法使其正常工作.问题已经解决了.但是,如果有人可以解释为什么OAuth在确切的日期和时间上如此严格,我将不胜感激.

The solution was synchronizing the server time. Server time was only 40 seconds different to atomic clock, but after sending ticket to my hosting provider I managed to get it working. Problem has gone. However, if someone could explain why does OAuth is such rigourous on the precise date and time, I would be grateful.

这篇关于Google API:"invalid_grant",但代码未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆