Google 驱动器 API 上传时返回 404
Google Drive API returning 404 on upload
我正在尝试通过发出 XMLHttpRequest 使用 Google Drive v3 API 将数据上传到文件。
我使用文件选取器 API 获取了一个文件的 ID,其中 return 是“0BzAI5S8IJebrUnQtYWFSVzhPdVk”,因此上传 URL 应该是“https://www.googleapis.com/upload/drive/v3/files/0BzAI5S8IJebrUnQtYWFSVzhPdVk?uploadType=resumable” .
在我发出 PUT 请求后,它 return 是一个 404 状态代码,响应文本是 "Not Found"。
我以前试过,它 return 状态为 200,我能够从响应中获取位置 header,但现在我无法获取 200 状态代码来自任何文件。
这是我的代码:
// 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();
无论我选择哪个文件,它总是输出404作为状态码。我遵循它在 https://developers.google.com/drive/v3/web/resumable-upload 中的说明,但它没有提及您为什么会收到 404 响应。有帮助吗?
原来我做错了事。我想更新一个文件,而不是上传一个。我应该按照 here 的解释使用 "PATCH"
,而不是使用 "PUT"
。这返回了带有 Location
header 的 200 响应以进行实际更改。
我正在尝试通过发出 XMLHttpRequest 使用 Google Drive v3 API 将数据上传到文件。
我使用文件选取器 API 获取了一个文件的 ID,其中 return 是“0BzAI5S8IJebrUnQtYWFSVzhPdVk”,因此上传 URL 应该是“https://www.googleapis.com/upload/drive/v3/files/0BzAI5S8IJebrUnQtYWFSVzhPdVk?uploadType=resumable” .
在我发出 PUT 请求后,它 return 是一个 404 状态代码,响应文本是 "Not Found"。
我以前试过,它 return 状态为 200,我能够从响应中获取位置 header,但现在我无法获取 200 状态代码来自任何文件。
这是我的代码:
// 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();
无论我选择哪个文件,它总是输出404作为状态码。我遵循它在 https://developers.google.com/drive/v3/web/resumable-upload 中的说明,但它没有提及您为什么会收到 404 响应。有帮助吗?
原来我做错了事。我想更新一个文件,而不是上传一个。我应该按照 here 的解释使用 "PATCH"
,而不是使用 "PUT"
。这返回了带有 Location
header 的 200 响应以进行实际更改。