使用 Drive API 和 PHP 将文件上传到 Teamdrives

Upload File to Teamdrives with Drive API and PHP

我想将文件上传到我的 teamdrive 但失败了。上传到我的驱动器有效。

我使用本地文件调用该函数,数组包含我的 Teamdrive 中的文件夹 ID 和 Team Drive ID。 $service a Google_Service_Drive 对象和 $client a Google_Client

我使用选项 supportsTeamDrives。

如果我尝试使用 listFiles,Teamdrives 也不存在。

如何通过 PHP 中的 API 访问 Teamdrives?

此版本现在有效:

function uploadGD($local_file, $folderid = NULL, $teamdrive = NULL)
{
    global $service;
    global $client;
    try {

    // Call the API with the media upload, defer so it doesn't immediately return.
        $client->setDefer(true);
        //$request = $service->files->create($file);

        $optParams = array(
            'fields' => 'id',
            'supportsTeamDrives' => true,
        );

            $request = $service->files->create(new Google_Service_Drive_DriveFile(array(
            "name" => basename($local_file),
            "teamDriveId" => $teamdrive,
            "parents" => $folderid,
            "mimeType" => mime_content_type($local_file))), $optParams);

        // Create a media file upload to represent our upload process.
        $media = new Google_Http_MediaFileUpload(
          $client,
          $request,
          mime_content_type($local_file),
          null,
          true,
          1 * 1024 * 1024
        );
        $media->setFileSize(filesize($local_file));

        // Upload the various chunks. $status will be false until the process is
        // complete.
        $status = false;
        $handle = fopen($local_file, "rb");
        while (!$status && !feof($handle)) {
          $chunk = fread($handle, $chunkSizeBytes);
          $status = $media->nextChunk($chunk);
         }

        // The final value of $status will be the data from the API for the object
        // that has been uploaded.
        $result = false;
        if($status != false) {
          $result = $status;
        }

        fclose($handle);
        // Reset to the client to execute requests immediately in the future.
        $client->setDefer(false);

        return "google|" . $result["id"];
    } catch (Exception $e) {
            return "Fehler:".$e->getMessage();
    }

}

错误信息显示:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "File not found: 0AHUD0ou-txfUUk9PVA.",
    "locationType": "parameter",
    "location": "fileId"
   }
  ],
  "code": 404,
  "message": "File not found: 0AHUD0ou-txfUUk9PVA."
 }
}

"File not found: 0AHUD0ou-txfUUk9PVA.",

基本上意味着您正在验证的用户无权访问有问题的文件,因为找不到它。您应该执行 files.list 以查看用户有权访问哪些文件。

如果您使用服务帐户进行身份验证,则需要确保该服务帐户已被授予访问团队云端硬盘帐户的权限,然后才能访问文件。