如何使用Google Drive API将文件传输到其他用户的云端硬盘? [英] How can I use the Google Drive API to transfer a file to another user's Drive?

查看:498
本文介绍了如何使用Google Drive API将文件传输到其他用户的云端硬盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试将目前属于我的Google文档转移到其他用户的Google云端硬盘的根目录。这两个帐户都在同一个域中,我使用服务帐户来访问所有用户。



起初我通过修改权限来尝试这样做。这会让第二个用户访问该文件,但不会将其放入其驱动器的根目录或任何其他位置。如果用户想看到它,他们将不得不进行搜索。然后,我发现一些信息提到使用文件服务的修补方法来更改元数据该文件,从而启动传输,但这没有奏效。



我唯一确定的是属于我自己和用户的根文件夹的ID我想将文件传输到。这是我试图做的但失败的。

 #注意:service是连接到域服务帐户

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'])

我无法分辨是否有任何错误,因为我没有收到任何错误消息。



如何去做到这一点将不胜感激。

解决方案

好的,我想通了。它看起来像我只是使用错误的API。你必须先插入新的权限,然后你必须删除旧的权限。这会将文件发送到新用户的Drive的根目录。

 #先插入新权限
new_permission = {
'value':'email_of_new_owner@mydomain.com',
'type':'user',
'role':'所有者'
}

run_new_permission = service.permissions()。insert(fileId ='FILE_ID_GOES_HERE',body = new_permission).execute()
#然后删除旧权限
service.permissions()。delete(fileId =' FILE_ID_GOES_HERE',permissionId ='PERMISSION_ID_GOES_HERE')。execute()


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.

解决方案

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()

这篇关于如何使用Google Drive API将文件传输到其他用户的云端硬盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆