问题描述
我在一家公司工作了几个星期,有了开发 PHP 内联网(作为网站)的想法,使用 Google Api 管理谷歌驱动器中的一些谷歌表。
循序渐进:
- 用户使用 gmail 地址连接到应用程序以访问网站。 所以我在安全方面使用了 google oauth 2.0 和 openid,仅用于连接和验证(应用程序验证电子邮件是否正确并具有特定域:[email protected] 而不是 [email protected])
-此后应用程序需要在特定的另一个 gmail 地址(例如:[email protected])上的谷歌驱动器中显示和管理一些目录和谷歌电子表格文件
我已经连接到我的 PHP 应用程序,并且我已经成功地使用服务帐户修改了一个简单的电子表格测试。
编辑:所以我使用了以下链接的服务帐户:https://github.com/googleapis/google-api-php-client/blob/master/docs/oauth-server.md#delegating-domain-wide-authority-to-the-service-account
我编写了以下代码来仅显示我在 [email protected] 谷歌驱动器中的文件:
<?PHP
require __DIR__ .'/vendor/autoload.PHP';
putenv('GOOGLE_APPLICATION_CREDENTIALS=intranetCredentials.json');
$client = new Google_Client([
'timeout' => 2.0,'verify' => __DIR__ . '/cacert.pem'
]);
$client->useApplicationDefaultCredentials();
$client->setScopes(Google_Service_Drive::DRIVE);
$driveManager = new Google_Service_Drive($client);
// Print the names and IDs for up to 10 files.
$optParams = array(
'pageSize' => 1,'fields' => 'nextPagetoken,files(id,name)'
);
$results = $driveManager->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n",$file->getName(),$file->getId());
}
}
我想我正在路上,但是当我执行我的 PHP 应用程序时,我得到:“找不到文件”而我有谷歌表和谷歌文档文件
除了将域范围的权限委派给服务帐户之外,我已经按照链接完成了所有操作。
有什么想法吗?
解决方法
您需要记住,服务帐户不是您自己。服务帐户是一个虚拟用户帐户,它有自己的谷歌驱动器帐户。您没有找到任何文件,因为它没有任何文件。
- 上传文件
- 通过您自己的帐户与服务帐户共享文件。