Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a Google Drive file using JS client

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);
    });

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.

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"}]

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.

Any hints?

like image 305
Boris Jockov Avatar asked Mar 20 '13 19:03

Boris Jockov


People also ask

Can JavaScript delete files?

You can't delete a file when running JavaScript from the browser, but you can do it when running JavaScript from a server environment like NodeJS. When you need to delete a file using NodeJS, You can use the fs. unlink() or fs.

How do I force delete a file in Google Drive?

When you launch Google Drive on the web, click on the file once and hit the delete icon at the top. You will see the three-dot icon on the files in the case of Docs, Sheets, etc. Click on it and select Remove.


1 Answers

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);
like image 187
user2196048 Avatar answered Sep 21 '22 15:09

user2196048