ChristianFigueroa
ChristianFigueroa

Reputation: 804

Google Drive API returning 404 on upload

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.com/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.com/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.com/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.com/drive/v3/web/resumable-upload, but it mentions nothing of why you would receive a 404 response. Any help?

Upvotes: 2

Views: 2252

Answers (1)

ChristianFigueroa
ChristianFigueroa

Reputation: 804

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.

Upvotes: 8

Related Questions