我試圖使用rest API從谷歌驅動器批量洗掉檔案。因此,我正在構建批量洗掉請求的請求,我能夠使用類似的請求構建方法使用原始 XMLHttpRequest 在 Google Drive 上批量洗掉檔案來實作洗掉,但我試圖在不發送正文的情況下實作這一點,而不是在請求物件。我收到以下回應正文的錯誤 400
<HEAD>
<TITLE>Bad Request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Bad Request</H1>
<H2>Error 400</H2>
</BODY>
</HTML>
這是我失敗的請求物件
const _multipart = []
arrayOfFileIds.forEach((current) => {
const obj = {
body: 'Content-Type: application/http\n\n'
'DELETE https://www.googleapis.com/drive/v3/files/'
current '\nAuthorization: Bearer ' authToken
}
_multipart.push(obj)
})
const requestOptions = {
url: 'https://www.googleapis.com/batch/drive/v3',
method: 'POST',
headers: {
'Content-Type': 'multipart/mixed'
},
multipart: _multipart
}
和下面的請求物件正在作業
const boundary = 'END_OF_PART'
const separation = '\n--' boundary '\n'
const ending = '\n--' boundary '--'
const requestBody = arrayOfFileIds.reduce((accum, current) => {
accum = separation
'Content-Type: application/http\n\n'
'DELETE https://www.googleapis.com/drive/v3/files/'
current
'\nAuthorization: Bearer ' authToken
return accum
}, '') ending
const requestOptions = {
url: 'https://www.googleapis.com/batch/drive/v3',
method: 'POST',
headers: {
'Content-Type': 'multipart/mixed; boundary=' boundary
},
body: requestBody
multipart: _multipart
}
uj5u.com熱心網友回復:
改裝要點:
- 訪問令牌可以包含在請求標頭中。
- 將
Content-Type每個批處理請求從body.
當這些點反映到你的腳本中時,它變成如下。
修改后的腳本:
const _multipart = [];
arrayOfFileIds.forEach((current) => {
const obj = {
"Content-Type": "application/http",
body: "DELETE https://www.googleapis.com/drive/v3/files/" current "\n",
};
_multipart.push(obj);
});
const requestOptions = {
url: "https://www.googleapis.com/batch/drive/v3",
method: "POST",
headers: {
"Content-Type": "multipart/mixed",
Authorization: "Bearer " authToken,
},
multipart: _multipart,
};
筆記:
- 當我測驗上述修改后的腳本時,沒有出現錯誤。可以洗掉檔案。當您測驗上述腳本時,出現錯誤時,請再次確認腳本和訪問令牌。
參考:
- 要求
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/385813.html
