Evan Ruff
Evan Ruff

Reputation: 602

Google Drive Copying, Team Drive (Service) to My Drive (User)

I'm trying to do what I thought would be a simple operation but I'm having some issues with the Google APIs. I've got two Google Credentials, one from a service account and one Access/Refresh from a user who has authenticated to the application.

The Service Account has the Domain Wide Delegation.

On a Team Drive that the Service Account can access (Templates) I have a doc that I would like to copy to the User's My Drive (root). I have the ID of the folder through:

String userRootId = userCredential.drive().files().get( "root" ).setFields( "id" ).execute().getId();

Which returns the proper identifier. When I use the Service Account to Copy the file, I can make copies in the Team Drive all day. It's when I go to copy it to the user's drive that I get "File not found: $userRootId".

I'm assuming that this stems from the Service Account not being able to access the User's My Drive. How can I make this operation work? Has anyone attempted this?

Upvotes: 1

Views: 621

Answers (1)

Evan Ruff
Evan Ruff

Reputation: 602

Okay so I figured out a way that works for my implementation. I have the Access/Refresh token of the target user drive. If you don't have that, I'm not sure how you could make it work. The steps are as follows:

  1. Get the SERVICE ACCOUNT root, using:

    String serviceRootId = this.serviceManager.drive().files().get( "root" ).setFields( "id" ).execute().getId();
    
  2. Use the service account to copy the file

    • Remove the Team Drive
    • set the parent to the service root id. This removes it from the team drive so you can actually do something with it.
  3. Share the file to the user with "writer" permissions (this eliminates any domain issue)
  4. Now, using the USER'S token, copy the file you just shared
  5. Delete the file the service account shared with the user.

This all seems a bit silly, but I think it's the only way to do it given the current limitations in Google Drive. I would've expected the service account with the Domain Wide Delegation to be allowed to just copy the thing, but it appears not.

If there's a better way to this, I'm very excited to learn, but for now this seems to work.

Upvotes: 1

Related Questions