3

I've been trying to get the Google Drive PHP API set up to upload a simple file to a shared drive using the following code.

<?php
require_once '../../../php/Services/JSON.php';
require '../vendor/autoload.php';
require '../helper.php';

$chunkSizeBytes = 1048576;

$client = new Google_Client();
$client->setAuthConfig(__DIR__.'/SERVICE-ACCOUNT-CREDENTIALS.json');
$client->setApplicationName('Uploader');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setDefer(true);

$file = 'testUpload.txt';

$service = new Google_Service_Drive($client);
$params = [
    'fields' => 'id',
    'supportsAllDrives' => true
];
$req = $service->files->create(new Google_Service_Drive_DriveFile([
    'name' => $file,
    'teamDriveId' => 'DRIVE ID',
    'parents' => '1Ik-tFv8UaOmlnZ3ojgPPba0o3hauh_63',
    'mimeType' => Helper::get_mime_type($file)
]), $params);

$media = new Google_Http_MediaFileUpload($client, $req, Helper::get_mime_type($file), null, true, $chunkSizeBytes);
$media->setFileSize(filesize($file));

$status = false;
$fileHandler = fopen($file, 'rb');
while(!$status and !feof($fileHandler)) {
    $chunk = fread($fileHandler, $chunkSizeBytes);
    $status = $media->nextChunk($chunk);
}

fclose($fileHandler);
$client->setDefer(false);
echo "https://drive.google.com/open?id=".$status['id']."\n";

After running this code it gives me a link to a file, but when I visit the link it says I need permission to access the file. When I open the folder on the shared drive the file is nowhere to be seen, so it's being uploaded, but not to the correct place as far as I know. I want to make sure that this file is uploaded to the shared drive, and to the folder specified, but so far I haven't been able to do so. I know some parameters have been deprecated from the API, but I'm pretty sure all the parameters that I'm using are not deprecated. I'm unsure of what I'm doing wrong, so any additional guidance would be appreciated, thanks!

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
NewAtThis
  • 33
  • 4
  • [This](https://stackoverflow.com/a/56468780/2518525) might be a solution as it looks like a similar problem to do with shared drives? – Darren Aug 19 '20 at 05:53
  • There's also a way to set permissions through the API, you might have to make a separate call, passing the file ID returned by the initial upload method. – ADyson Aug 19 '20 at 06:54

1 Answers1

3

The servier account is not you. The service account is uploading the file there for it owns the file. If you want access to it have the service account grant you access to it though permissions.create

$optParams = array(                
   'emailMessage' => '[YourValue]',                  
   'sendNotificationEmail' => '[YourValue]',                  
   'supportsTeamDrives' => '[YourValue]',                  
   'transferOwnership' => '[YourValue]',
   'fields' => '*'
);

return $service->permissions->CreatePermissions($fileId, $optParams);

Also remember to add supportsAllDrives parameter to the initial file upload.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449