微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。