如何用Python调用Google APIs实现跨用户Google Drive文件转移?
跨用户Google Drive文件传输实现指南(Python)
核心说明
跨用户直接移动Drive文件不可行(文件所有权绑定原用户),但两种方案可以实现需求:
- 创建文件副本并共享给目标用户,或上传至目标用户共享的目录(通用方案,支持外部用户)
- 利用Google Workspace域范围授权,模拟目标用户身份操作其Drive(仅适用于同域内用户)
方案一:跨用户通用方案(含外部用户)
操作步骤
- 让目标用户(发件人)共享一个Drive文件夹给你的操作账号(脚本运行的邮箱),权限设为「编辑」或「内容管理者」
- 获取该共享文件夹的ID:打开文件夹后,URL中
folders/后的字符串即为ID - 调用Drive API完成文件复制或上传
代码示例
1. 复制已有Drive文件到目标用户共享目录
from googleapiclient.discovery import build from google.oauth2.credentials import Credentials # 加载已完成OAuth2认证的凭证(替换为你的凭证加载逻辑) creds = Credentials.from_authorized_user_file('token.json', ['https://www.googleapis.com/auth/drive']) drive_service = build('drive', 'v3', credentials=creds) def copy_to_target_folder(source_file_id, target_folder_id, target_user_email): # 创建文件副本并指定目标文件夹 copied_file = drive_service.files().copy( fileId=source_file_id, body={ 'parents': [target_folder_id], 'name': f"处理副本_{source_file_id}" # 自定义副本名称 } ).execute() # 给目标用户添加编辑权限(若共享文件夹已授权则可省略) permission_body = { 'type': 'user', 'role': 'writer', 'emailAddress': target_user_email } drive_service.permissions().create( fileId=copied_file['id'], body=permission_body, sendNotificationEmail=True # 可选:是否给目标用户发通知邮件 ).execute() return copied_file['id'] # 调用示例 source_file_id = "你的源文件ID" target_folder_id = "目标用户共享的文件夹ID" target_user_email = "发件人邮箱地址" copied_file_id = copy_to_target_folder(source_file_id, target_folder_id, target_user_email) print(f"副本已创建,ID:{copied_file_id}")
2. 直接上传本地处理后的文件到目标共享目录
from googleapiclient.http import MediaFileUpload def upload_to_target_folder(local_file_path, target_folder_id, target_user_email): file_metadata = { 'name': "处理后的附件", 'parents': [target_folder_id] } media = MediaFileUpload(local_file_path, resumable=True) uploaded_file = drive_service.files().create( body=file_metadata, media_body=media, fields='id' ).execute() # 给目标用户添加权限(可选) permission_body = { 'type': 'user', 'role': 'writer', 'emailAddress': target_user_email } drive_service.permissions().create( fileId=uploaded_file['id'], body=permission_body, sendNotificationEmail=True ).execute() return uploaded_file['id']
方案二:Google Workspace域内专属方案(域范围授权)
如果你的服务在企业Google Workspace域名内运行,可通过域管理员配置授权,直接模拟目标用户操作其Drive,无需目标用户提前共享文件夹。
操作步骤
- 在Google Cloud Console创建服务账号,下载JSON密钥文件
- 域管理员在Google Workspace Admin Console中,给该服务账号授权
https://www.googleapis.com/auth/drive范围 - 用服务账号密钥生成模拟目标用户的凭证,直接操作其Drive
代码示例
from googleapiclient.discovery import build from google.oauth2.service_account import Credentials from googleapiclient.http import MediaFileUpload # 服务账号密钥文件路径 SERVICE_ACCOUNT_KEY = "service-account-key.json" # 要模拟的目标用户邮箱(发件人邮箱) TARGET_USER_EMAIL = "sender@yourdomain.com" # 生成模拟目标用户的凭证 creds = Credentials.from_service_account_file( SERVICE_ACCOUNT_KEY, scopes=['https://www.googleapis.com/auth/drive'], subject=TARGET_USER_EMAIL ) drive_service = build('drive', 'v3', credentials=creds) # 上传文件到目标用户Drive根目录 def upload_to_user_drive(local_file_path): file_metadata = {'name': "处理后的附件"} media = MediaFileUpload(local_file_path, resumable=True) uploaded_file = drive_service.files().create( body=file_metadata, media_body=media, fields='id' ).execute() print(f"文件已上传至目标用户Drive,ID:{uploaded_file['id']}") return uploaded_file['id']
常见问题排查
- 权限调整失败:确认操作账号对目标文件夹有「编辑」权限,或服务账号已获得正确的域范围授权
- 外部用户无法访问:创建副本后必须给目标用户添加权限,或确保目标文件夹已共享给操作账号
- API调用报错:检查OAuth2凭证的权限范围是否包含
https://www.googleapis.com/auth/drive,避免使用只读范围
内容的提问来源于stack exchange,提问作者CHIRAQA
相关产品推荐
相关产品推荐

