1

我在使用 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);

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

4

1 回答 1

2

谜团已揭开。看来我们的直播环境还有 5 分钟的时间。这导致 OAuth2 身份验证出现问题。因此,如果您有同样的问题,请确保您的时间设置正确,最好是通过 NTP 自动设置!

于 2015-06-30T12:35:52.880 回答