javascript - 从刚刚检索到的 id 获取 Google Doc

标签 javascript java jsf google-drive-api ajax4jsf

我正在开发一个 Java - JSF 网络应用程序,我在其中显示了一个弹出窗口,以便让用户从 Google 云端硬盘中选择一个文档进行下载。

为此,我有一些 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();,它会返回一个空列表。

最佳答案

如果您想下载 Google 文档,请使用 files.export。能反射(reflect)下面的脚本试试看吗?

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

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

关于javascript - 从刚刚检索到的 id 获取 Google Doc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46239841/

相关文章:

spring - 如何处理AJAX请求中的 session 超时?

java - 为什么 tomcat 正在寻找 JSF 类?

JSF 和部分验证

javascript - 根据选择选项显示 div?

javascript - 嵌套开关在 React 路由器中不起作用

javascript - 单击外部关闭侧边栏

java - SonarQube - 为什么要在 catch 语句上设置 Coverage 指标

java - 尝试划分颜色来绘制彩虹 donut

java - 从 WAR 文件获取文件

javascript - Jquery .on() 不调用选择器