Google Drive API 复制团队驱动器中的文件

问题描述

我正在编写一个函数来将我的 Google Drive 中的一个文件夹复制到另一个文件夹中。该功能适用​​于常规驱动器空间中的文件夹。但是,当我在团队驱动器文件夹上运行它时,出现以下错误

Traceback (most recent call last):
  File "C:/Code/catpython/driveapi_utils.py",line 232,in <module>
    test_copy_folder()
  File "C:/Code/catpython/driveapi_utils.py",line 221,in test_copy_folder
    copy_folder(service,src='xxxxxx',File "C:/Code/catpython/driveapi_utils.py",line 211,in copy_folder
    copy_folder(service,file.get('id'),file.get('name'),dst_parent_id,**kwargs)
  File "C:/Code/catpython/driveapi_utils.py",line 201,in copy_folder
    fileId=clone.get("id"),AttributeError: 'nonetype' object has no attribute 'get'

我的代码如下:

def copy_folder(service,src,src_name,dst,**kwargs):
    """
    copies a folder. It will create a new folder in dst with the same name as src,and copies the contents of src into the new folder
    src: Source folder's id
    dst: Destination folder's id that the source folder is going to be copied to
    """

    page_token = None
    while True:
        response = service.files().list(q="'%s' in parents and trashed = false" % src,supportsAllDrives=True,spaces='drive',fields='nextPagetoken,files(id,name,mimeType)',pagetoken=page_token,includeItemsFromAllDrives=True,).execute()
        dst_parent_id = create_folder(service,**kwargs)
        for file in response.get('files',[]):
            # Process change
            print('Found file: %s (%s,%s)' % (file.get('name'),file.get('mimeType')))
            if not file.get('mimeType') == 'application/vnd.google-apps.folder':
                clone = None
                try:
                    clone = service.files().copy(fileId=file.get('id'),body={'title': 'copiedFile','parents': [{'kind': "drive#fileLink",'id': dst_parent_id}],'supportsAllDrives': True,}).execute()
                except HttpError as httpe:
                    pass
                try:
                    service.files().update(
                        fileId=clone.get("id"),addParents=dst_parent_id,removeParents=src,fields="id,parents",supportsAllDrives=True
                    ).execute()
                except HttpError as httpe:
                    pass
            else:
                print('drilling into %s' % file.get('name'))
                copy_folder(service,**kwargs)
        page_token = response.get('nextPagetoken',None)
        if page_token is None:
            break

我调试了代码,我认为复制 api 抛出了一个错误

<HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/1n7h04M6Rpz3m2J6MGZq8trlnADEBFLrK/copy?alt=json returned "File not found: 1n7h04M6Rpz3m2J6MGZq8trlnADEBFLrK.". Details: "File not found: 1n7h04M6Rpz3m2J6MGZq8trlnADEBFLrK.">

但是该文件确实存在,并且相同的代码适用于我个人空间中的文件。我可以完全访问团队驱动器,正如您从代码中看到的那样,我可以查询文件并在其中创建文件夹。

可能缺少什么?或者有没有更好的方法来实现这一点?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)