google-api-php-客户"error":"unauthorized_client"

google-api-php-client "error": "unauthorized_client"

我们正在尝试构建一个服务来为我们的 gsuite 域导出 google 驱动器文件。

我们正在与 https://github.com/google/google-api-php-client

合作

我们创建了一个服务用户,下载并保存了用户凭据,并授予服务用户全域访问权限。

最简单的例子如下(就像我发现的每个文档一样):

putenv('GOOGLE_APPLICATION_CREDENTIALS='/service-account-credentials.json');

$client = new \Google_Client();
$client->setAuthConfig('service-account-credentials.json');

$client->setScopes('https://www.googleapis.com/auth/drive.file');

$client->useApplicationDefaultCredentials();
$client->setSubject('admin@mydomain.com');

$service = new \Google_Service_Drive($client);

$service->files->listFiles(); 

一旦我们包含 $client->setSubject('admin@mydomain.com'); 行,它就会删除以下错误:

{
    "error": "unauthorized_client",
    "error_description": "Client is unauthorized to retrieve access tokens using this method."
}

有任何人有想法,或者可以共享此问题的代码示例。这对我们来说是一个真正的障碍,现在我不知道为什么会出现这个错误

我终于成功了

经过几次尝试后,我意识到我将全域访问权限委托给了错误的客户端 ID。我一直认为这应该与我在脚本中使用的 "client_email" 相同。但这真的(真的,真的,真的)很重要,这就是 "client_id"(顺便说一下,正如文档中提到的)。这不是电子邮件或字符串,它只是您在为服务帐户创建密钥时获得的一个简单数字。