2017-09-15 78 views
1

我正在开发一个Java-JSF Web应用程序,其中显示一个弹出窗口,以便让用户从Google Drive中选择一个文档进行下载。从刚刚检索到的id获取Google Doc

为了做到这一点,我有一些JS代码:

<script src="filepicker.js"></script> 
<script> 
    function initPicker() { 
     var picker = new FilePicker({ 
      apiKey: 'MY_API_KEY', 
      clientId: my_client_id, 
      buttonEl: document.getElementById('pick'), 
      onSelect : function(file) { 
         if (file.id) { 
          sendLinkToBean(file.id); 
         } else { 
          alert('Unable to download file.'); 
         } 
        } 
       }); 
    } 
</script> 

<a4j:jsFunction name="sendLinkToBean" action="#{gPPersonFormBean.downloadFile()}"> 
    <a4j:param name="param1" assignTo="#{gPPersonFormBean.fileId}"/> 
</a4j:jsFunction> 

的file.id到达豆,我设法得到它,如图G.Drive的API:

public void downloadFile(){ 
    try { 
     Drive driveService = getDriveService(); 
     OutputStream outputStream = new ByteArrayOutputStream(); 
     driveService.files().get(fileId) .executeMediaAndDownloadTo(outputStream); 
    } catch (IOException e) { 
     String mess = "downloadFile(): " 
       + (e.getMessage()!=null?". "+e.getMessage():"") 
       + (e.getCause()!=null?". "+e.getCause():""); 
     logger.error(mess); 
    } 
} 

但我得到FileNotFoundException:

com.google.api.client.http.HttpResponseException: 404 Not Found 
{ 
"error": {"theID" 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "notFound", 
    "message": "File not found: 1tvnGWDCse4TFQwIvqEDTlvv2cebfs0C2JBtjTP5A42I.", 
    "locationType": "parameter", 
    "location": "fileId" 
    } 
    ], 
    "code": 404, 
    "message": "File not found: theID." 
} 
} 

有谁知道为什么会发生这种情况? 在此先感谢。

编辑:我刚刚收到的ID与Google给出的ID与文件链接进行了比较,它们完全相同。

编辑2:如果我做driveService.files().list();它返回一个空的列表。

回答

0

正如Tanaike说,我不得不使用出口而不是GET也executeAndDownloadTo代替executeMediaAndDownloadTo

所以它的工作是:

String fileId = "## file ID ##"; 
OutputStream outputStream = new ByteArrayOutputStream(); 
driveService.files().export(fileId, "application/pdf") 
    .executeAndDownloadTo(outputStream); 
1

如果您要下载Google文档,请使用files.export。你能反映下面的脚本并尝试它吗?

String fileId = "## file ID ##"; 
OutputStream outputStream = new ByteArrayOutputStream(); 
driveService.files().export(fileId, "application/pdf") 
    .executeMediaAndDownloadTo(outputStream); 
  • 此示例脚本和细节信息是here

如果我误解了你的问题,我很抱歉。

+0

谢谢您的回答,但它给了同样的错误,404文件找不到 – Aldeguer

+0

@Aldeguer对于给您带来的不便,我深表歉意。刚才我看到了你的EDIT2。我可以问你想下载的文件是否在你的Google Drive中? – Tanaike

+0

是的,我从Google Picker和JS制作的驱动器文档中选择它。我已经尝试了两种,一种是由我创建的文件,另一种是由同伴创建的文件,但与我共享。没有人工作 – Aldeguer

相关问题