Javascript 使用原始XMLHttpRequest在Google驱动器上批量删除文件
我正在chrome扩展中使用此功能。Javascript 使用原始XMLHttpRequest在Google驱动器上批量删除文件,javascript,google-chrome-extension,google-drive-api,Javascript,Google Chrome Extension,Google Drive Api,我正在chrome扩展中使用此功能。 具有文件ID的简单请求将删除单个文件 xhr.open('DELETE', 'https://www.googleapis.com/drive/v2/files/' + ID, true); 如果我想从Google Drive中删除多个文件,我将通过ID的数组循环发送相同的请求,只发送多次。 大多数请求都成功了,但如果我的请求数超过7-8,则有些请求会失败,并出现错误代码403(我认为这代表禁止)。 通常,如果我有12个文件要删除,两个或三个文件将失败。
具有文件ID的简单请求将删除单个文件
xhr.open('DELETE', 'https://www.googleapis.com/drive/v2/files/' + ID, true);
如果我想从Google Drive中删除多个文件,我将通过ID的数组循环发送相同的请求,只发送多次。大多数请求都成功了,但如果我的请求数超过7-8,则有些请求会失败,并出现
错误代码403
(我认为这代表禁止)。通常,如果我有12个文件要删除,两个或三个文件将失败。
(当我重复请求时,它们将被删除) Google Drive是否有一些防止节流的保护措施,以及如何删除多个文件?…
延迟计时器(例如100毫秒)是不可取的,因为我可能有数百个文件要删除,而处理它需要10-30秒
doc没有提到删除多个文件,只有一个这是使用REST API从Google Drive中删除文件的批处理请求的示例代码:
var arrayOfFileIds; // array of id's of the files you want to delete
//notice that currently you can only batch up to 100 requests.
var authToken; //your OAuth2 token.
var xhr = new XMLHttpRequest;
var boundary = "END_OF_PART";
var separation = "\n--"+boundary + "\n";
var ending = "\n--" + boundary + "--";
var requestBody = arrayOfFileIds.reduce((accum,current)=>{
accum += separation +
"Content-Type: application/http\n\n" +
"DELETE https://www.googleapis.com/drive/v2/files/" +
current +
"\nAuthorization: Bearer " + authToken;
return accum;
},"") + ending;
xhr.onload = ()=>{
console.log(xhr.response);
//handle the response
};
xhr.open("POST", "https://www.googleapis.com/batch/drive/v2", true);
xhr.setRequestHeader("Content-Type","multipart/mixed; boundary=" + boundary);
xhr.send(requestBody);
或者,您可以向每个请求(在上面的
requestBody
中)添加content id
头,以标识批处理响应中的各个响应。这是使用REST API从Google Drive删除文件的批处理请求的示例代码:
var arrayOfFileIds; // array of id's of the files you want to delete
//notice that currently you can only batch up to 100 requests.
var authToken; //your OAuth2 token.
var xhr = new XMLHttpRequest;
var boundary = "END_OF_PART";
var separation = "\n--"+boundary + "\n";
var ending = "\n--" + boundary + "--";
var requestBody = arrayOfFileIds.reduce((accum,current)=>{
accum += separation +
"Content-Type: application/http\n\n" +
"DELETE https://www.googleapis.com/drive/v2/files/" +
current +
"\nAuthorization: Bearer " + authToken;
return accum;
},"") + ending;
xhr.onload = ()=>{
console.log(xhr.response);
//handle the response
};
xhr.open("POST", "https://www.googleapis.com/batch/drive/v2", true);
xhr.setRequestHeader("Content-Type","multipart/mixed; boundary=" + boundary);
xhr.send(requestBody);
或者,您可以向每个请求(在上面的
requestBody
中)添加content-id
头,以标识批处理响应中的各个响应。如果请求的状态代码是错误,您可以简单地重试请求…@wOxxOm-yea,我正在这样做:)…但我认为这不是正确的方法。。。他们(G-DEV)一定期望发生类似的事情。文档建议使用和/或。此外,考虑迁移到API。v3@Iv安诺科诺科,我会调查的it@Iv文档页面中缺少批处理请求的ánNokonoko示例:)bummerWell,如果请求的状态代码为错误,您可以简单地重试该请求…@wOxxOm yea,我正在这样做:)…但我认为这不是正确的方法。。。他们(G-DEV)一定期望发生类似的事情。文档建议使用和/或。此外,考虑迁移到API。v3@Iv安诺科诺科,我会调查的it@Iv文档页面中缺少批处理请求的ánNokonoko示例:)bummerthanx用于发布示例。类似于charm.thanx,用于发布示例。工作起来很有魅力。