使用JS客户端删除Google Drive文件

使用JS客户端删除Google Drive文件

问题描述:

我尝试使用Google云端硬盘文档中的示例。
所以代码是:

I tried using the example from Google Drive documentation. So the code is :

var request = gapi.client.drive.files.delete({
    'fileId' : someFileId
    });

    request.execute(function(resp) 
    {
        console.log(resp);
    });

该应用程序已正确安装,并且正在使用drive.file作用域。
问题是该文件未被删除。它仍然存在于云端硬盘用户界面中,无法再打开或下载。文件已损坏。

The app is installed properly and I'm using drive.file scope. The problem is that the file is not deleted. It is still present in the Drive UI and cannot be opened anymore or downloaded. File is corrupted.

发送的请求不是DELETE https://www.googleapis.com/drive/v2/files/fileId ,如文档中所述。这是POST https://www.googleapis.com/rpc?key=API_KEY 。正文包含一个JSON数组:

The request being sent is not the DELETE https://www.googleapis.com/drive/v2/files/fileId as was stated in docs. It is a POST https://www.googleapis.com/rpc?key=API_KEY. The body contains a JSON array:

[{"jsonrpc":"2.0","id":"gapiRpc","method":"drive.files.delete","params":{"fileId":"someFileId"},"apiVersion":"v2"}]

响应包含一个空的JSON对象。响应中没有错误,页面中没有JS错误。 APIs Explorer成功删除文件。

Response contains one empty JSON object. There are no errors in the response and there are no JS errors in the page. The APIs Explorer successfully deletes the file.

任何提示?

Try a XMLHttpRequest instead:

var xmlReq = new XMLHttpRequest();
xmlReq.open('DELETE', 'https://www.googleapis.com/drive/v2/files/' + fileId + '?key=' + apiKey);
xmlReq.setRequestHeader('Authorization', 'Bearer ' + accessToken);