1
votes

I am trying to create a folder and change the owner using Google Drive API PHP Client. Folder gets created properly, and I can set any permission aside from owner, which returns an error. My code:


function createFolder($name, $parents, $mimeType)
{
    $client = getClient();
    $service = new Google_Service_Drive($client);
    $folder = new Google_Service_Drive_DriveFile();
    $folder->setName($name);
    if ($parents) {$folder->setParents(array($parents));}
    $folder->setMimeType($mimeType);
    $created_folder = $service->files->create($folder);
    $created_folder_id = $created_folder->getId();


    $ownerPermission = new Google_Service_Drive_Permission();
    $ownerPermission->setEmailAddress("shared@******.com");
    $ownerPermission->setType('user');
    $ownerPermission->setRole('owner');

    $service->permissions->create($created_folder_id, $ownerPermission, array('transferOwnership' => 'true'));
    return $created_folder_id;

}

And the return:

Fatal error: Uncaught Google\Service\Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "The transferOwnership parameter must be enabled when the permission role is 'owner'.",
    "locationType": "parameter",
    "location": "transferOwnership"
   }
  ],
  "code": 403,
  "message": "The transferOwnership parameter must be enabled when the permission role is 'owner'."
 }

Am I not setting the transferOwnership parameter correctly?

2
have you tried $ownerPermission->setTransferOwnership(true); - DaImTo
I did, it returns that setTransferOwnership is an undefined method - DLateef
Have you find the answer to solve your problem bro? - Hygison Brandao
@HygisonBrandao yes see the last comment I wrote at the bottom, was able to answer this myself and marked it as such. - DLateef

2 Answers

2
votes

@DLateef If you want to make the file shareable, you may want to read more about Permission resource in Drive API. For "shared" to be true, each file permission needs to specify a role, type, and email address or domain. As an owner of the file (Docs, Sheets, etc.), you will need to provide the appropriate permission to be set to.

Here is an example, using the Permissions.create

POST https://www.googleapis.com/drive/v3/files/{fileId}/permissions?key={YOUR_API_KEY}

{
 "role": "reader",
 "type": "user",
 "emailAddress": "[email protected]"
}

Response from the Drive Files.get:

GET https://www.googleapis.com/drive/v3/files/{fileId}?fields=appProperties%2CfileExtension%2Ckind%2CmimeType%2Cshared&key={YOUR_API_KEY}


{
 "kind": "drive#file",
 "mimeType": "application/vnd.google-apps.document",
 "shared": true
}

However, if you are unable to switch to the Drive v3, you can still use the Permission.insert from Drive v2 to do the job.

Or else, you can also try ownerPermission->setTransferOwnership(true);

1
votes

I was able to get this to work by separating the code for creating the folder an transferring ownership. If called as part of the same createFolder function I get the error described above, but transferring ownership in a separate function, after folder is created, works fine.