'刷新OAuth2令牌时出错,消息:' {"错误" :" invalid_grant" }''在现场网站上,但适用于当地环境

时间:2015-06-30 09:46:20

标签: php google-admin-sdk google-client-login

我对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);

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

1 个答案:

答案 0 :(得分:2)

神秘解决了。看来我们的现场环境将来有5分钟的时间。这导致OAuth2身份验证出现问题。因此,如果您遇到同样的问题,请确保您的时间设置正确,最好通过NTP自动设置!

相关问题