cairabbit
cairabbit

Reputation: 65

Google Drive REST API Get file returns 404 with scope "drive.file" only

I'm using google drive REST API V3 to access the file. The basic api url is: https://www.googleapis.com/drive/v3/files/fileid

And the header contains key: Authorization with Bearer access token obtained from oauth.

However, if you create an normal google doc, this works. the response is:

{
"id": "1v3V0BWcWtYu_K9OLm1rffHA3vj5Bz8SY-0-JpU7CiEc",
"name": "test",
"mimeType": "application/vnd.google-apps.document",
"parents": [
    "0AP75Wn-e9WDqUk9PVA"
]
}

But if I created a google doc from google template, this returns 404. see below:

{
"error": {
    "errors": [
        {
            "domain": "global",
            "reason": "notFound",
            "message": "File not found: 14gHUCFTrwHaoZ88Kr0Qnn8OsUdzcU037GK7v-JI9L1I.",
            "locationType": "parameter",
            "location": "fileId"
        }
    ],
    "code": 404,
    "message": "File not found: 14gHUCFTrwHaoZ88Kr0Qnn8OsUdzcU037GK7v-JI9L1I."
}
}

Upvotes: 1

Views: 5600

Answers (8)

Alexis Aguilar
Alexis Aguilar

Reputation: 1

I get a solution with this:

// Replace with your own project number from console.developers.google.com.
// See "Project number" under "IAM & Admin" > "Settings"
var appId = "1234567890";

var picker = new google.picker.PickerBuilder()
            ...
            .setAppId(appId)
            ...
            .build();

Upvotes: 0

Jhkcia
Jhkcia

Reputation: 405

As the documentation says drive.file scope is used to view and manage Google Drive files and folders that you have opened or created with this app.

I also encouraged 404 Error and I finally found the solution, In my case, I used Google Drive Picker in the front-end for selecting files, and then I send the file-ids to the backend for downloading them and I had the same 404 File not found Error on the backend when I try to download files.

I see the error because I did not set appId in my picker API. so when I try to instantiate googlePicker I set the app key and then everything is fixed.

// Replace with your own project number from console.developers.google.com.
// See "Project number" under "IAM & Admin" > "Settings"
var appId = "1234567890";

var picker = new google.picker.PickerBuilder()
            ...
            .setAppId(appId)
            ...
            .build(); 

Now both backend and front-end use the same application in google and when the user selects a file from picker API it is available in all applications.

More info: https://developers.google.com/picker/docs/

Upvotes: 3

Josep Vidal
Josep Vidal

Reputation: 2681

2021 answer here, in my case the issue (404 for some files) existed because the Google project number I was using was wrong.

And to make it harder, some accounts did work perfectly, and some did not using just this scope:

https://www.googleapis.com/auth/drive.file

As it is indicated in the google docs of the picker:

// Replace with your own project number from console.developers.google.com.
// See "Project number" under "IAM & Admin" > "Settings"
const appId = "1234567890";

After the project number was fixed, everything started working flawlessly (No need for https://www.googleapis.com/auth/drive scope as its a free-for-all scope that gives permissions for everything).

Cheers.

Upvotes: 7

palisade
palisade

Reputation: 486

In my case it was about setting Drive.files().get(googleId).setSupportsAllDrives(true) to gain access to files on shared drives.

Upvotes: 0

Femi
Femi

Reputation: 64700

Filling this in here for posterity: when using the drive.file permission with a file picker, the url/token you receive will only work if the picker was hosted on the same domain as is configured in the Google Developer Console Drive SDK configuration page (https://console.developers.google.com/apis/api/drive.googleapis.com/drive_sdk).

If you are hosting it on any other domain, you'll get a 404 if you try and use the URL to directly download the file: you'll need to use drive.readonly or drive permissions.

Upvotes: 3

cairabbit
cairabbit

Reputation: 65

After I requested more permissions, the problem solved. Nothing changed.

Upvotes: 0

cairabbit
cairabbit

Reputation: 65

I did a bit test and found that the "drive.file" scope actually has some problem with getting file metadata/content. From the documentation, the "drive.file" allow the app to access the file that created by this app, or user explicitly open the file with this app, from from drive or from google doc. When I testing this, actually this doesn't work as expect. I upload a few files, one of the files can be accessed by my app, but the others not. I remembered when I publish my app, the google drive team reviewed my app and rejected a few times with the scope, finially the app passed review with "drive.file" scope only.

Did anyone encounter same issue?

Upvotes: 1

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

Somehow this is not the case. I created a google doc using the resume template. I obtained the fileID and run it against files.get. I got a 200 response:

{
 "kind": "drive#file",
 "id": "1y1h2vHYwrb57O-ein7VhqMwwJKtr4pP4N4bBow7-vus",
 "name": "resumeTemplate",
 "mimeType": "application/vnd.google-apps.document"
}

BTW, the URI requst for files.get is

https://www.googleapis.com/drive/v3/files/fileId

not

https://www.googleapis.com/drive/v3/files/

Upvotes: 0

Related Questions