Use the Google Drive API to change the owner of files created through the account service
P粉680487967
P粉680487967 2024-01-06 11:03:37
0
1
551

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粉680487967
P粉680487967

reply all(1)
P粉571233520

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template