“提供的文件 ID 不可用。”使用带有 python 脚本的驱动器 API 来复制文件时

问题描述 投票:0回答:1

我正在尝试编写 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'}]">

我尝试了多个文件,我什至以编辑者的身份与链接共享这些文件。什么都没用,身份证也没有用。

python google-drive-api copy drive
1个回答
0
投票

尝试使用 supportAllDrives:

file = service.files().copy(
            fileId=fileID,
            body=file,
            fields='id',
            supportsAllDrives=True).execute()
© www.soinside.com 2019 - 2024. All rights reserved.