When attempting to transfer ownership I get the following error:
"domain": "global",
"reason": "invalidSharingRequest",
"message": "Bad Request. User message: \"You can't change the owner of this item.\""
Here is my code:
use Google_Client;
use Google_Service_Drive;
use Google_Service_Drive_DriveFile;
use Google_Service_Drive_Permission;
public function uploadDocument() {
$FOLDER_ID = 'my_folder_ID_string';
$client = new Google_Client();
$client->setAuthConfig(base_path('service_account_credentials.json'));
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'Test.pdf',
'parents' => [$FOLDER_ID]
));
$content = file_get_contents(public_path('tmp/Test.pdf'));
$file = $service->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => 'application/pdf',
'uploadType' => 'multipart',
'fields' => 'id'
));
// Transfer Ownership
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setRole('owner');
$newPermission->setType('user');
$newPermission->setEmailAddress('[email protected]');
$optParams = array('transferOwnership' => 'true');
$service->permissions->create($file->id, $newPermission, $optParams);
}
The folder is successfully uploaded to Google Drive in the shared folder (owner is '[email protected]', service account is 'editor'), however the owner of the uploaded file is the service account and the editor is '[email protected]'.