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

时间:2017-11-24 13:29:50

标签: php google-drive-api google-php-sdk

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

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

我使用supportTeamDrives选项。

如果我尝试使用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."
 }
}

1 个答案:

答案 0 :(得分:2)

  

"找不到档案:0AHUD0ou-txfUUk9PVA。",

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

如果您使用服务帐户进行身份验证,则需要确保已授予服务帐户访问团队驱动器帐户的权限,然后才能访问这些文件。