我正在尝试编写 python 脚本,以使用 Drive API 和服务帐户来复制和编辑 Google Drive 上的文件以实现许可目的。
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
from google.oauth2.service_account import Credentials
def copy_file_to_folder(file_id, destination_folder_id):
creds = Credentials.from_service_account_file('service_account.json')
try:
service = build('drive', 'v3', credentials=creds)
file = service.files().get(fileId=file_id).execute()
file['parents'] = [destination_folder_id]
copied_file = service.files().copy(fileId=file_id, body=file, fields='id').execute()
return copied_file['id']
except HttpError as error:
print(f'An error occurred: {error}')
return None
if __name__ == "__main__":
file_id = 'File ID I wanted to copy'
destination_folder_id = 'Destination folder ID to put the copy in'
copy_file_to_folder(file_id, destination_folder_id)
此脚本运行时返回以下错误:
An error occurred: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files/[File ID]/copy?fields=id&alt=json returned "The provided file ID is not usable.". Details: "[{'message': 'The provided file ID is not usable.', 'domain': 'global', 'reason': 'fileIdNotUsable', 'location': 'file.id', 'locationType': 'other'}]">
我尝试了多个文件,我什至以编辑者的身份与链接共享这些文件。什么都没用,身份证也没有用。
尝试使用 supportAllDrives:
file = service.files().copy(
fileId=fileID,
body=file,
fields='id',
supportsAllDrives=True).execute()