找不到文件错误Google Drive API

use*_*491 3 google-drive-api

我正在使用Drive REST API下载文件.我正在使用文件ID发出GET请求,我得到了一个file not found exception.

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "File not found: xxxxx",
    "locationType": "other",
    "location": "file"
   }
  ],
  "code": 404,
  "message": "File not found: xxxxx"
 }
} 
Run Code Online (Sandbox Code Playgroud)

我也生成了apikey,我在我的GET请求中使用它.文件确实存在,所以我不确定我在这里缺少什么.

vin*_*zee 9

就我而言,我只是没有向我的服务帐户授予对该文件夹的访问权限。只需通过网络界面共享即可解决问题。

使用什么电子邮件地址:

在此处检查您的服务帐户的电子邮件地址:

https://console.cloud.google.com/iam-admin/serviceaccounts?project=NAME_OF_PROJECT&supportedpurview=project

电子邮件地址将如下所示:

name-of-service-account@name-of-project.iam.gserviceaccount.com 
Run Code Online (Sandbox Code Playgroud)

  • 这也是我的问题,我没有在任何地方看到它的记录,谢谢! (3认同)

Ala*_*Dea 7

如果您收到如下回复:

<HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/REDACTED_FILE_ID/copy?alt=json returned "File not found: REDACTED_FILE_ID.". Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: REDACTED_FILE_ID.', 'locationType': 'parameter', 'location': 'fileId'}]">
Run Code Online (Sandbox Code Playgroud)

并且 fileId 指向共享驱动器上的文件,您需要在请求参数中包含supportsSharedDrives=true。

Google 在其实施共享驱动器支持文章中提供了更多详细信息。

下面是一个使用 Python 创建副本的小示例:

<HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/REDACTED_FILE_ID/copy?alt=json returned "File not found: REDACTED_FILE_ID.". Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: REDACTED_FILE_ID.', 'locationType': 'parameter', 'location': 'fileId'}]">
Run Code Online (Sandbox Code Playgroud)

为了运行它,您需要将其混合到Google Drive API 的 Python 快速入门 的示例代码中。

  • `supportsAllDrives=True` 对我来说很重要,因为我试图获取/更新*共享*文件。 (4认同)

use*_*491 5

这已经解决了.在发出文件元数据的GET请求时,我没有提供正确的access_token.我重新生成了授权码access_token,我的代码现在正在运行.


小智 5

确保范围是校正

var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: ['https://www.googleapis.com/auth/drive.file',
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/drive.file',
    'https://www.googleapis.com/auth/drive.metadata'
  ]
});
Run Code Online (Sandbox Code Playgroud)

Drive API 声明了以下范围。选择您要授予 APIs Explorer 的那些。

来源:https : //developers.google.com/apis-explorer/#p/drive/v3/drive.files.get


小智 5

我会检查您尝试检索元数据的文件是否是团队驱动器的一部分;如果是,那么您必须supportsTeamDrives=True在请求中进行设置。

  • supportTeamDrives 在 v3 中已弃用,请参阅supportsAllDrives 答案 (4认同)