You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Google Drive API获取共享云端硬盘的文件元数据?

问题

我有一段通过FileID获取自身及共享给我的文件元数据的Google Drive API调用代码,但获取共享云端硬盘内文件的元数据时,会出现“File Not Found”错误。我已使用corpora='AllDrives'参数的list接口获取到包含共享云端硬盘的文件ID列表,请问如何在get接口中兼容所有云端硬盘?

相关代码:

def GetFileInfoById(TheFileId):

    service = authenticate()
    # Call the Drive v3 API
    results = service.files().get(fileId=TheFileId,
        fields="id, name, md5Checksum, webViewLink").execute()
    pprint(results)

if __name__ == "__main__":
  GetFileInfoById('FILE ID Here')

报错信息:

raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/FILE ID Here?fields=id%2C+name%2Cmd5Checksum%2CwebViewLink&alt=json returned "File not found: 1xJImDFvjIEJlLKUxFqzarm6fSWV8Vlxe.". Details: "[{'message': 'File not found: FILE ID Here.', 'domain': 'global', 'reason': 'notFound', 'location': 'fileId', 'locationType': 'parameter'}]">

解决方案

要让files().get()接口兼容共享云端硬盘的文件,需在调用时添加两个关键参数:

  • supportsAllDrives=True:允许API访问所有云端硬盘(包括共享的团队硬盘)
  • includeItemsFromAllDrives=True:确保查询范围覆盖所有可访问的云端硬盘内容

修改后的代码如下:

def GetFileInfoById(TheFileId):

    service = authenticate()
    # Call the Drive v3 API
    results = service.files().get(
        fileId=TheFileId,
        fields="id, name, md5Checksum, webViewLink",
        supportsAllDrives=True,
        includeItemsFromAllDrives=True
    ).execute()
    pprint(results)

if __name__ == "__main__":
  GetFileInfoById('FILE ID Here')

参数说明:

  • supportsAllDrives=True:必须设置该参数,否则API默认仅访问用户自身的云端硬盘,无法识别共享硬盘中的文件ID
  • includeItemsFromAllDrives=True:配合前者,确保API查询时包含所有可访问的云端硬盘资源

内容的提问来源于stack exchange,提问作者Todd B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 01:01:04