【问题标题】:get Google Doc from id just retrieved从刚刚检索到的 id 获取 Google Doc
【发布时间】:2017-09-15 12:40:34
【问题描述】:

我正在开发一个 Java - JSF Web 应用程序,我在其中显示一个弹出窗口,以便让用户从 Google Drive 中选择一个文档来下载它。

为此,我有一些 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 到达 Bean,我尝试获取它,如 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(); 它会返回一个空列表。

【问题讨论】:

    标签: javascript java jsf google-drive-api ajax4jsf


    【解决方案1】:

    如果您想下载 Google 文档,请使用 files.export。能否反映以下脚本并尝试一下?

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

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

    【讨论】:

    • 感谢您的回答,但它给出了同样的错误,404 File not found
    • @Aldeguer 很抱歉给您带来不便。我刚才看到了你的 EDIT2。请问您要下载的文件是否在您的 Google Drive 中?
    • 是的,我从使用 Google Picker 和 JS 制作的驱动器文档中选择它。我都试过了,一个文件是我创建的,另一个文件是由一个伙伴创建的,但与我共享。没有人工作
    • @Aldeguer 关于driveService.files().list();为空,你为请求设置了哪些字段?并且,作为示例,您能否在 Google Drive 上创建新的 Google 文档并尝试使用您的脚本检索文件列表?如果列表中包含创建的文件,您的脚本可能能够下载该文件。
    • 嗯,我没有得到你的问题,我在这方面很菜鸟,对此感到抱歉。我做到了:com.google.api.services.drive.Drive.Files.List list = driveService.files().list();。我会试试你的样品并告诉你结果。谢谢大家
    【解决方案2】:

    正如 Tanaike 所说,我必须使用 export 而不是 get,还要使用 executeAndDownloadTo 而不是 executeMediaAndDownloadTo

    所以它起作用了:

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

    【讨论】:

      猜你喜欢
      • 2014-08-23
      • 2013-03-31
      • 2018-05-14
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多