search

Home  >  Q&A  >  body text

Use the Google Drive API to change the owner of files created through the account service

I use a service account to upload files to a shared folder in Google Drive.

After some time I discovered that files owned by the service account were consuming the service account's drive storage (my fault) and now I have run out of the service account's drive space.

I have delegated domain-wide permissions to the service account, so the new files will be owned by me and use my personal storage quota.

Do this: Delegate domain-wide permissions to a service account

And how to use the API key for the Google Drive API from PHP via google/apiclient

To avoid future errors and confusion, I want to change the owner of the old file. I keep getting this error:

{ "error": { 
    "code": 400, 
    "message": "Bad Request. User message: "You can't change the owner of this item."", 
    "errors": [ {
       "message": "Bad Request. User message: "You can't change the owner of this item."", 
       "domain": "global", 
       "reason": "invalidSharingRequest" 
    } ] 
  } 
}

This is my code using PHP client

$client = new Google_Client();
$client->setApplicationName('My Name');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setAuthConfig($my_credentials);
$client->setAccessType('offline');
//$client->setSubject('my_personal_account');

$service = new Google_Service_Drive($client);

$newPermission = new Google_Service_Drive_Permission();
$newPermission->setEmailAddress('my_personal_account');
$newPermission->setType('user');
$newPermission->setRole('owner');

$service->permissions->create(
  $fileId, 
  $newPermission, 
  array("fields" => "id", "transferOwnership" => true)
);

I get the same error regardless of whether I use setSubject in the client or not. I have tried using

$newPermission->setRole('writer');
$newPermission->setPendingOwner(true);

But without success.

P粉680487967P粉680487967354 days ago580

reply all(1)I'll reply

  • P粉571233520

    P粉5712335202024-01-07 00:10:19

    File ownership can only be transferred between accounts on the same domain. This error occurs because the service account and your account do not belong to the same domain.

    If you have access to the shared drive and are able to add a user with permission to add files, add a service account and let it move files to the shared drive.

    Related

    Other related

    reply
    0
  • Cancelreply