';刷新OAuth2令牌时出错,消息:';{“error”:“invalid_grant”}''


'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' on live site, but works on local environment

我在使用Google Admin SDK时遇到了一个非常奇怪的问题(我使用的是Google API PHP客户端库)。我可以成功地添加组和用户,通常在我的本地开发机器上使用API,但当我把它放在实时网站上时,它会崩溃,并出现以下错误:

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }''

我将它与服务帐户结合使用。为此,我生成了一个p12密钥和一个客户端JSON文件,这两个文件都包含在项目中。我也使用一个真实的帐户作为委托管理帐户。

我正在使用以下代码连接到Google SDK:

<?php
$this->client = new Google_Client();
$this->client->setAuthConfigFile(STORAGE_PATH.'client_secrets.json');
$this->client->addScope(static::$scopes);
$cred = new Google_Auth_AssertionCredentials(
    static::$service_account_email,
    static::$scopes,
    file_get_contents(STORAGE_PATH.'TRICS-key.p12'));
$cred->sub = static::$delegated_admin;
$this->client->setAssertionCredentials($cred);
$this->directory_service = new Google_Service_Directory($this->client);

有人知道可能出了什么问题吗?

谜团解开。我们的生活环境似乎在未来5分钟内就到了。这导致OAuth2身份验证出现问题。因此,如果您有同样的问题,请确保您的时间设置正确,最好是通过NTP自动设置!