带有服务帐户的PHP Google Drive API

问题描述 投票:1回答:0

我想读取公司私有域中的Google云端硬盘文件夹,以便可以访问和处理Google表单的结果。

这是一个有效的PHP代码:

putenv('GOOGLE_APPLICATION_CREDENTIALS='path/to/drive-api.json');

$client = new \Google_Client();
$client->useApplicationDefaultCredentials();        
$client->addScope('https://www.googleapis.com/auth/drive');     

$client->setAccessType('offline');   
//$client->setSubject('[email protected]');
$httpClient = $client->authorize();

但是使用此代码,我只能访问服务帐户的驱动器空间。

我想取消注释“ setSubject()”行,以便我可以模拟真实用户并访问真实文件夹。 但是这样做给我这个错误:

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

我的服务帐户具有域范围的委派,并且我已将范围访问权限添加到Gsuite域的管理控制台。 我已经把这里写的一切都做完

那么我该如何使用服务帐户模拟用户? 请记住,出于安全原因,用户不能共享“ mydomain.com”之外的内容,并且服务帐户位于“ .iam.gserviceaccount.com”上

php google-drive-api google-oauth google-api-php-client
© www.soinside.com 2019 - 2024. All rights reserved.