1

I have uploaded some test files to my google drive using their website and my gmail account (x@gmail.com). The files are in "My Drive" and not shared drive. I am then trying to write a google drive java api and oauth authorization code flow based program to transfer the ownership of these files to my another gmail account(y@gmail.com). i.e both the accounts belong to @gmail.com.

I am logged in to my application using x@gmail.com using authorization code workflow and my app is trying to access Google APIs with my access token. I am able to create a new permission on test files to make y@gmail.com a "writer" of the file. But when I try to make "y" an owner, I get error message. This is despite of x being the owner and being the one whose access token is used.

How do I transfer the ownership to another account?

    Message changePerms(@PathVariable(name = "fileId") String fileId, @PathVariable(name = "userEmail") String newOwnerEmail) throws Exception {
        Drive drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getOauthCredentials())
                .setApplicationName("my-app").build();

        Permission newOwnerPermission = new Permission();
        newOwnerPermission.setEmailAddress(newOwnerEmail);
        newOwnerPermission.setType("user");
        newOwnerPermission.setRole("owner"); // Works if this is "writer" so my credentials and auth is working
       
        drive.permissions().create(fileId,newOwnerPermission).setTransferOwnership(true).execute();
}
Error : 
POST https://www.googleapis.com/drive/v3/files/19wvNc4TmXhODJa7Iy3Qq97UAD8fJrVg/permissions?transferOwnership=true
{
  "code": 400,
  "errors": [
    {
      "domain": "global",
      "message": "Bad Request. User message: \"You can't change the owner of this item.\"",
      "reason": "invalidSharingRequest"
    }
  ],
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
WillMcavoy
  • 1,795
  • 4
  • 22
  • 34
  • 1
    What scope are you using? Are you sure its not a directory you are trying to change ownership of? Can you try and do it in two steps? Add them as writer then update them to owner? Why are you not using a service account? Are you going to have other users doing this besides yourself? – Linda Lawton - DaImTo Nov 26 '21 at 08:11
  • What is the type of file you are attempting to change ownership of, and what Drive is it in (user Drive, shared Drive)? – Rafa Guillermo Nov 26 '21 at 08:25
  • `Why are you not using a service account` @DaImTo not sure how a service account will be helpful in this case, can you explain that further? – Rafa Guillermo Nov 26 '21 at 08:25
  • I was trying with PDF file , just tried it with txt and doc files and the above code worked. The files are in User drive. How does type matter and how to get around it? – WillMcavoy Nov 26 '21 at 08:27
  • @WillMcavoy Its not an problem it should work with them both on the same domain. that being gmail.com user domain. – Linda Lawton - DaImTo Nov 26 '21 at 08:32
  • @WillMcavoy can you check that the owner you are transferring it to does have diskspace on their drive account? – Linda Lawton - DaImTo Nov 26 '21 at 08:36
  • @DaImTo Yes the other account has space. I verified multiple times , pdf, jpeg does not work txt doc works fine without any code change. I am trying to build this as a multi user web app. The access token of user will be stored in session attribute. Do you know how can I initialize Credentials for Drive Service using access token stored in the user session? The GoogleCredential class had that option but its deprecated. Ref : Answer https://stackoverflow.com/questions/67020565/spring-5-oauth2-with-google-refresh-access-token-after-401-return – WillMcavoy Nov 26 '21 at 09:00
  • It works with text, but not with pdf and jpg. OK thats strange. Wonder if i can recreate this – Linda Lawton - DaImTo Nov 26 '21 at 09:03
  • @DalmTo It only consistently works with Doc files for me sorry I mistakenly mentioned txt. I am running into issues with txt files as well – WillMcavoy Nov 27 '21 at 06:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239628/discussion-between-willmcavoy-and-daimto). – WillMcavoy Nov 27 '21 at 06:48
  • Have you tried the same operations via the Try This API feature? – Rafa Guillermo Nov 29 '21 at 08:36
  • Yes , I see the same error – WillMcavoy Nov 29 '21 at 08:49
  • When this worked, were you using @gmail.com addresses or Workspace accounts? The docs say that [You can only transfer ownership of files to people within your work or school.](https://support.google.com/drive/answer/2494892) – Rafa Guillermo Dec 14 '21 at 09:13

1 Answers1

0

I think you can't transfer ownership files if they have been uploaded to your Drive. There was a mention of this on the help page, but that one line was removed earlier this year (though I can't say I know why as it appears that nothing has changed).

The current version of the help page is here, but as you can see using the Wayback Machine that only one month ago the following line was still present:

You can only transfer ownership of Google files and folders.

So I would say that this is probably the reason.

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54