[英]java google drive API unable to download binary file
我有工作代碼來列出我的 Google Drive 文件夾中包含二進制文件的文件,但我在嘗試下載時遇到錯誤:
GET https://www.googleapis.com/download/drive/v3/files/1udFizffA_0M4jSUBJbDI6uNVdPVhdymW?alt=media
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"location" : "alt",
"locationType" : "parameter",
"message" : "Only files with binary content can be downloaded. Use Export with Docs Editors files.",
"reason" : "fileNotDownloadable"
} ],
"message" : "Only files with binary content can be downloaded. Use Export with Docs Editors files."
}
相關代碼如下:
Drive.Files f = driveService.files();
HttpResponse httpResponse = null;
Drive.Files.Get get = f.get(file.getId());
httpResponse = get.executeMedia();
我還嘗試使用以下方式導出:
httpResponse = f.export(file.getId(),"application/octet-stream").executeMedia();
這也沒有用。
有人可以幫我弄清楚我錯過了什么嗎?
file.get方法確實返回文件數據,但僅適用於二進制文件類型。 這就是此錯誤消息告訴您的內容。
只能下載包含二進制內容的文件。 使用 Export with Docs Editors 文件。
String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.executeMediaAndDownloadTo(outputStream);
所有其他文件類型是谷歌文檔、谷歌表格。 查看此頁面了解更多信息: mimetype
需要從 google mime 類型導出為標准 mime 類型。 如果您查看manage-downloads#java頁面,您將找到有關如何使用此方法的文檔示例。
String fileId = "1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().export(fileId, "application/pdf")
.executeMediaAndDownloadTo(outputStream);
此方法的技巧是確保您設置正確的mimetype
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.