我正在嘗試將元資料添加到我之前上傳到 Google Drive(文本)的檔案中,元資料是標題和描述,但是當我發送檔案時它不會更改其名稱或任何內容。但是(元資料)似乎已添加到文本內容中。即就像它只是更新該檔案的文本
代碼:
$.ajax({
url: "https://www.googleapis.com/upload/drive/v2/files/---?UploadType=multipart",
data: {
title: "name...",
// other parameters indicated by google
},
headers: {
"Authorization": "Bearer ..."
},
contentType: "application/json", //as indicated by google
type: "PUT",
success: function () {
// ...
}
});
在谷歌文本檔案中出現:
title=name...
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您的 Google 云端硬碟中有一個檔案。
- 您想使用帶有 ajax 的 Drive API v2 更新檔案的檔案名。
- 您的訪問令牌可用于更新檔案的檔案名。
在這種情況下,如何進行以下修改?
修改點:
- 為了更新檔案元資料,請使用
PUT https://www.googleapis.com/drive/v2/files/fileId. - 請使用檔案元資料的 JSON 物件作為字串值,使用
JSON.stringify().
當這些點反映在你的放映腳本中時,它變成如下。
修改后的腳本:
$.ajax({
url: "https://www.googleapis.com/drive/v2/files/{fileId}",
data: JSON.stringify({title: "name..."}),
headers: {
"Authorization": "Bearer ###your access token###",
},
contentType: "application/json",
type: "PUT",
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
如果您想使用 Drive API v3,請將上述腳本修改如下。
$.ajax({ url: "https://www.googleapis.com/drive/v3/files/{fileId}", data: JSON.stringify({name: "name..."}), headers: { "Authorization": "Bearer ###your access token###", }, contentType: "application/json", type: "PATCH", success: function (data) { console.log(data); }, error: function (data) { console.log(data); } });
參考:
- 檔案:Drive API v2 的更新
- 檔案:Drive API v3 更新
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/521588.html
