'File not found error Google Drive API

I am using the Drive REST API to download a file. I am making a GET request using the file id and I get a 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"
 }
} 

I have also generated the apikey and I am using it in my GET request.The file does exist so I am unsure what I am missing here.



Solution 1:[1]

This is resolved. I wasn't providing the correct access_token when making the GET request for file metadata. I regenerated the authorization code, access_token and my code is working now.

Solution 2:[2]

Make sure scopes are corrects

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'
  ]
});

Drive API declares the following scopes. Select which ones you want to grant to APIs Explorer.

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

Solution 3:[3]

In my case, I simply had not given access to the folder to my service account. Simply sharing it via the web interface solved the problem.

What email address to use:

Check the email address of your service account here:

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

The email address will look like this:

[email protected] 

Solution 4:[4]

I would check to see if the file you are trying to retrieve the metadata for is part of a team drive; if it is then you have to set supportsTeamDrives=True in the request.

Solution 5:[5]

If you are getting a response like this:

<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'}]">

And the fileId points to a file on a Shared Drive, you'll need to include supportsSharedDrives=true in your request parameters.

Google provides more detail on this in their Implement shared drive support article.

Here's a small example with Python for creating a copy:

googledrive = build('drive', 'v3', credentials=creds)

copy_response = googledrive.files().copy(
    fileId=TEMPLATE_SPREADSHEET_ID,
    body={
        "parents": [
            report_folder_id,
        ],
        'title': report_title
    },
    supportsAllDrives=True
).execute()

In order to run that, you'll want to mix it into the example code from the Python Quickstart for Google Drive API.

Solution 6:[6]

In my case, I was using the file client_secret.json that I had created with the gmail address I use all the time, but I was creating the credentials with another e-mail.

Solution 7:[7]

I got to this question because I was getting a "File not found" error when using the update method.

I was using this line in Python to execute the method:

files_res.update(fileId=f_id, body={"name": f_name}).execute()

I could list the shared file and its parent fine, but update failed, no matter what the scope with this error:

...
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "File not found: 1YqV...",
    "locationType": "parameter",
    "location": "fileId"
   }
  ],
  "code": 404,
...

I added the supportsAllDrives=True param to the method and it worked:

files_res.update(fileId=f_id, body={"name": f_name}, supportsAllDrives=True).execute()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2676491
Solution 2 Sanjay Nishad
Solution 3 vinzee
Solution 4 ofahmy93
Solution 5
Solution 6 vinzee
Solution 7 Zach Young