使用JS客户端删除Google云端硬盘文件

时间:2013-03-20 19:02:34

标签: javascript google-drive-api

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

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

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

应用已正确安装,我正在使用drive.file范围。 问题是该文件未被删除。它仍然存在于Drive UI中,无法再打开或下载。文件已损坏。

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

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

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

任何提示?

1 个答案:

答案 0 :(得分:5)

尝试使用XMLHttpRequest:

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