如何使用Drive API获取我自己的文件夹文件?

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

我找到了 Google Drive API 文档,它们都谈到了 OAuth,这将允许我的用户授予对其帐户文件的访问权限,但我想通过 API 提供我的文件。我怎样才能做到这一点?另外,我正在使用 PHP 和这个库 https://github.com/googleapis/google-api-php-client

我当前的代码

<?php 
use Google\Client;
use Google\Service\Drive;
$files = [];
try{
require_once 'vendor/autoload.php';
$client = new Google\Client();
$client->setApplicationName("MYNAME");
$client->setDeveloperKey("MYKEY"); 
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$pageToken = null;
do {
            $response = $driveService->files->listFiles(array(
                'q' => "mimeType='image/jpeg'",
                'spaces' => 'drive',
                'pageToken' => $pageToken,
                'fields' => 'nextPageToken, files(id, name)',
            ));
            foreach ($response->files as $file) {
                printf("Found file: %s (%s)\n", $file->name, $file->id);
            }
            array_push($files, $response->files);

            $pageToken = $response->pageToken;
} while ($pageToken != null);
var_dump($files);
 } catch(Exception $e) {
       echo "Error Message: ".$e;
    }

?>

这给我带来了不够的文件权限。

php google-cloud-platform google-drive-api
1个回答
0
投票

要使用 Google Drive API 访问 Google Drive 中您自己的文件而不需要通过 OAuth 进行用户授权,您应该使用服务帐户。服务帐户专为服务器到服务器交互而设计,可以访问与服务帐户关联的帐户拥有的文件。

设置方法如下:

1.创建服务帐户

  1. 转到 Google Cloud Console
  2. 创建新项目或选择现有项目。
  3. 导航到 IAM 和管理 > 服务帐户
  4. 单击创建服务帐户
  5. 填写服务帐户详细信息,然后单击创建
  6. 授予服务帐户必要的角色(例如 Google 云端硬盘的“查看者”或“编辑者”)。
  7. 分配角色后单击完成
  8. 在列表中找到您的服务帐户,然后单击它以查看其详细信息。
  9. Keys选项卡下,单击添加密钥> JSON。这将下载一个包含您的服务帐户凭据的 JSON 文件。

2.分享您的 Google 云端硬盘文件夹

如果您希望服务帐户访问特定文件或文件夹:

  1. 转到 Google 云端硬盘。
  2. 右键单击您希望服务帐户访问的文件夹或文件。
  3. 点击分享
  4. 在“与人员和群组共享”字段中,输入服务帐户的电子邮件(类似于
    [email protected]
    )。
  5. 授予服务帐户必要的权限(查看者、编辑者等)。

3.更新您的 PHP 代码

现在,更新您的 PHP 代码以使用服务帐户凭据:

<?php
require_once 'vendor/autoload.php';

use Google\Client;
use Google\Service\Drive;

$files = [];

try {
    $client = new Client();
    $client->setApplicationName("MYNAME");
    $client->setScopes(Drive::DRIVE);
    $client->setAuthConfig('path/to/your-service-account.json'); // Path to your downloaded JSON key file
    $client->setSubject('[email protected]'); // Optional: the email of the user you want to impersonate (if applicable)

    $driveService = new Drive($client);
    $pageToken = null;

    do {
        $response = $driveService->files->listFiles(array(
            'q' => "mimeType='image/jpeg'",
            'spaces' => 'drive',
            'pageToken' => $pageToken,
            'fields' => 'nextPageToken, files(id, name)',
        ));

        foreach ($response->files as $file) {
            printf("Found file: %s (%s)\n", $file->name, $file->id);
        }
        
        array_push($files, $response->files);
        $pageToken = $response->nextPageToken; // Fixed typo here
    } while ($pageToken != null);

    var_dump($files);
} catch (Exception $e) {
    echo "Error Message: " . $e->getMessage();
}
?>

主要变化

  • 服务帐户 JSON:从 JSON 密钥文件加载服务帐户凭据。
  • 共享:确保您要访问的文件或文件夹与服务帐户共享。
  • 权限:确保服务帐户具有访问文件的正确权限。

故障排除

  • 如果您仍然遇到权限问题,请仔细检查服务帐户是否已被授予对 Google 云端硬盘中的文件或文件夹的必要权限。
  • 确保在 Google Cloud Console 中的API 和服务 > 库下为您的项目启用了 API。搜索“Google Drive API”并确保其已启用。

此设置应允许您的 PHP 应用程序访问 Google 云端硬盘中的文件,而无需用户 OAuth 权限。如果您需要进一步的帮助,请告诉我!

© www.soinside.com 2019 - 2024. All rights reserved.