1

I want to upload files to my teamdrive but it fail. Upload to my drive works.

I call the function with a local file, array with folder id in my Teamdrive and the Team Drive ID. $service a Google_Service_Drive Object and $client a Google_Client

I use the option supportsTeamDrives.

If I try listFiles the Teamdrives also not exist.

How can I acces Teamdrives over the API in PHP?

This Version works now:

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();
    }

}

The Error Message shows:

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

1 Answers1

2

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

basically means that the user you are authenticating with does not have access to the file in question there for can not find it. You should do a files.list in order to see which files a user has access to.

If you are authenticating with a service account you need to make sure that the service account has been granted access to the team drive account then it will be able to access the files.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks for you Reply. The User has Permission to the Team Drive and the App use Google_Service_Drive::DRIVE (Full Access) Did I need some other Scopes for Teamdrives? The User has Full Access to the Team Drive – yannik995 Nov 27 '17 at 07:11
  • then do a file.list to see what files the user has access to. If they dont have access to the file in question you need to grant them access – Linda Lawton - DaImTo Nov 27 '17 at 07:24
  • If I use $service->teamdrives->listTeamdrives( I get all Teamdrives. I am the creator of the Teamdrives so I have Fullaccess – yannik995 Nov 27 '17 at 10:49
  • are you logging in with your own account then? using Oauth2? check and make sure that you have access to this file id 0AHUD0ou-txfUUk9PVA that doesnt look like a google file id to me but they could have changed it. – Linda Lawton - DaImTo Nov 27 '17 at 11:07
  • Yes. 0AHUD0ou-txfUUk9PVA is the TeamDrive ID. They are shorter and I have Access. If I use ListTeamdrives I also get that ID. – yannik995 Nov 27 '17 at 12:07
  • Yes but a timedrive id is not a file id. https://developers.google.com/drive/v3/web/manage-sharing – Linda Lawton - DaImTo Nov 27 '17 at 12:39
  • Ich changed the start parameter: uploadGD($dest, array("0B4kqsF_KuM8CLUg4TVF6OHZOSm8"), "0AHUD0ou-txfUUk9PVA"); First ID is the Folder, and the Second the Teamdrive, like here: https://stackoverflow.com/questions/45327769/how-can-i-create-a-folder-within-a-team-drive-with-the-google-api This Script work, but my Version won't – yannik995 Nov 28 '17 at 07:47
  • I removed array( "mimeType" => mime_content_type($local_file), "uploadType" => "resumable"), and now it works, but I don't know why. – yannik995 Nov 28 '17 at 08:06