How can I use the Google Drive API to transfer a f

2019-03-04 10:26发布

I am trying to transfer a Google Doc that currently belongs to me and move it to the root of another user's Google Drive. Both account are in the same domain and I'm using a Service Account to access all users.

At first I tried doing this by modifying the permissions. This would give the second user access to the file but it would not place it into the root of their drive or anywhere else. If the user wanted to see it they would have to do a search.

I then found some information that mentioned using the patch method of the files service to change the metadata of the file and thus initiate the transfer, but this hasn't worked.

The only thing I have for certain are the ID's of the root folders belonging to myself and the user I want to transfer the files to. Here's what I tried to do but failed.

# Note: "service" is the connection to the domain service account 

service.files().patch(fileId='FILE_ID_GOES_HERE',
                                     body={'parents':[{'isRoot':True,'kind':'drive#parentReference','id':'user_two_root_folder_id'}]},
                                     removeParents=['user_one_root_folder_id'],
                                     addParents=['user_two_root_folder_id'])

I can't tell if anything is wrong because I'm not getting any error messages.

Any ideas as to how to go about this would be greatly appreciated.

2条回答
等我变得足够好
2楼-- · 2019-03-04 11:13

OK, I figured it out. It looks like I was just using the wrong API. You have to first insert the new permission then you have to delete the old permission. This will send the file to the root of the new user's Drive.

# Insert new permission first
new_permission = {
          'value': 'email_of_new_owner@mydomain.com',
          'type': 'user',
          'role': 'owner'
          }

run_new_permission = service.permissions().insert(fileId='FILE_ID_GOES_HERE',body=new_permission).execute()
# Then delete old permission
service.permissions().delete(fileId='FILE_ID_GOES_HERE', permissionId='PERMISSION_ID_GOES_HERE').execute()
查看更多
The star\"
3楼-- · 2019-03-04 11:28

Does it return any kind of error?

I don't think you can "move" the file like that. Try file.insert to his drive then delete from your own.

I have never heard of anyone trying to patch the location of a file to someone else. TBH I can't see how that could work, because you would have to have service authenticated with both your account and his account which is not possible. Even though as you say you are using a service account with access to both account, I seriously drought that the API can support this.

You will need to do this in two steps, insert -> to him Delete -> from you

Update: I just heard from a colleague who is a Google Drive expert, he said he thought it was broken (patching location). He did a quick test and verified this is a broken feature. All it returns is 403 - insufficient permissions

查看更多
登录 后发表回答