admin管理员组

文章数量:1431918

I'm trying to upload data to a file using the Google Drive v3 API by making a XMLHttpRequest.

I get the id of a file using the File Picker API which returns "0BzAI5S8IJebrUnQtYWFSVzhPdVk", so that the upload URL should be "".

After I make the PUT request, it returns a 404 status code with "Not Found" as the responseText.

I've tried it before and it would return with status 200 and I was able to get the Location header from the response, but now I can't get a 200 status code from any file.

Here's my code:

// Log the user in using attachClickHandler ()

var id = "0BzAI5S8IJebrOFVMTU5Od183Q2M";
var access_token = gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token;

var xhr = new XMLHttpRequest();

xhr.open('PUT', '/' + id + '?uploadType=resumable');
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);

xhr.onreadystatechange = function() {
    if (this.readyState == 4) console.log(this.status);
};
xhr.send();

No matter which file I choose, it always outputs 404 as the status code. I follow what it says at , but it mentions nothing of why you would receive a 404 response. Any help?

I'm trying to upload data to a file using the Google Drive v3 API by making a XMLHttpRequest.

I get the id of a file using the File Picker API which returns "0BzAI5S8IJebrUnQtYWFSVzhPdVk", so that the upload URL should be "https://www.googleapis./upload/drive/v3/files/0BzAI5S8IJebrUnQtYWFSVzhPdVk?uploadType=resumable".

After I make the PUT request, it returns a 404 status code with "Not Found" as the responseText.

I've tried it before and it would return with status 200 and I was able to get the Location header from the response, but now I can't get a 200 status code from any file.

Here's my code:

// Log the user in using attachClickHandler (https://developers.google./identity/sign-in/web/reference#googleauthattachclickhandlercontainer-options--onsuccess-onfailure)

var id = "0BzAI5S8IJebrOFVMTU5Od183Q2M";
var access_token = gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token;

var xhr = new XMLHttpRequest();

xhr.open('PUT', 'https://www.googleapis./upload/drive/v3/files/' + id + '?uploadType=resumable');
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);

xhr.onreadystatechange = function() {
    if (this.readyState == 4) console.log(this.status);
};
xhr.send();

No matter which file I choose, it always outputs 404 as the status code. I follow what it says at https://developers.google./drive/v3/web/resumable-upload, but it mentions nothing of why you would receive a 404 response. Any help?

Share Improve this question asked May 21, 2017 at 10:14 ChristianFigueroaChristianFigueroa 8046 silver badges15 bronze badges 13
  • From the page you link, you are missing a couple of mandatory http headers. Otherwise, 404 would suggest there is a problem with the file ID you are using. Are you sure it's correct and is writeable by the logged in user? – pinoyyid Commented May 21, 2017 at 11:22
  • They're definitely writable. I've tried with three different files on my own account that I own, so I can write to all three. Which other headers am I missing? The X-Content-Upload-Length/Type headers are optional and the Content-Type header is required only if I send metadata. – ChristianFigueroa Commented May 21, 2017 at 18:42
  • The other was content-length. Not sure if it's the problem, just ruling out all possibilities. Try another operation on the ID such as a files.get, just to double check the ID. – pinoyyid Commented May 21, 2017 at 20:15
  • File.get returns a 200 response with the proper information. It also works on the API Explorer on developers.google./drive/v3/reference/files/get so it can't be the ID. – ChristianFigueroa Commented May 21, 2017 at 21:30
  • Also the browser doesn't let me change the Content-Length header. The browser sets the header automatically and throws an error if I try to change it manually: Refused to set unsafe header "Content-Length" – ChristianFigueroa Commented May 21, 2017 at 21:32
 |  Show 8 more ments

1 Answer 1

Reset to default 8

Turns out I was trying to do the wrong thing. I wanted to update a file, not upload one. Instead of using "PUT", I should've been using "PATCH" as explained here. This returned a 200 response with the Location header to make the actual change.

本文标签: javascriptGoogle Drive API returning 404 on uploadStack Overflow