2013-03-20 58 views
5

我嘗試使用Google Drive documentation中的示例。 因此,代碼爲:使用JS客戶端刪除Google Drive文件

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

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

該應用程序正確安裝,我使用drive.file範圍。 問題是該文件未被刪除。它仍然存在於雲端硬盤用戶界面中,無法再打開或下載。文件已損壞。

正在發送的請求不是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"}] 

響應包含一個空的JSON對象。響應中沒有錯誤,頁面中沒有JS錯誤。 API資源管理器成功刪除該文件。

任何提示?

回答

5

嘗試,而不是一個XMLHttpRequest:

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

感謝。這是訣竅。我猜APIs瀏覽器並沒有真正使用JS客戶端。 我已經提交了一個錯誤,希望它很快得到修復。 https://code.google.com/p/google-api-javascript-client/issues/detail?id=77 – 2013-03-21 16:44:50

+0

我遇到問題。看到我的帖子,http://stackoverflow.com/questions/16522158/delete-file-in-gdrive-with-javascript。我可以使用此解決方案刪除文檔,以便使用API​​使用列表找不到它,但它仍會保留在Google Drive中並且已損壞。意思是我可以看到它,但沒有打開或移除它。 – arpo 2013-05-14 07:02:34

+0

我正在用'gapi.client.init'方法初始化Google Drive api。 我如何獲得aaccess令牌? – Apuleius 2017-05-24 05:03:08

相關問題