(Java)下载URL不起作用

我正在努力尝试使用谷歌驱动器API下载文件。 我只是编写应该从我的驱动器下载文件到我的计算机上的代码。 我终于到了一个我经过身份validation的阶段,可以查看文件元数据。 出于某种原因,我仍然无法下载文件。 我得到的downLoadURL看起来像:

https://doc-04-as-docs.googleusercontent.com/docs/securesc/XXXXXXXXXXXXXX/0B4dSSlLzQCbOXzAxNGxuRUhVNEE?e=download&gd=true

当我运行代码或将其复制并粘贴到浏览器中时,此URl不会下载任何内容。 但是,在浏览器中,当我删除URL的“&gd = true”部分时,它会下载该文件。

我的下载方法直接来自google drive API文档:

public static InputStream downloadFile(Drive service, File file) { if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) { try { System.out.println("Downloading: "+ file.getTitle()); return service.files().get(file.getId()).executeMediaAsInputStream(); } catch (IOException e) { // An error occurred. e.printStackTrace(); return null; } } else { // The file doesn't have any content stored on Drive. return null; } } 

有谁知道这里发生了什么?

提前致谢。

由于您使用的是Drive v2,因此您可以通过HttpRequest对象获取InputStream的不同方法(也在文档中 )。

 /** * Download a file's content. * * @param service Drive API service instance. * @param file Drive File instance. * @return InputStream containing the file's content if successful, * {@code null} otherwise. */ private static InputStream downloadFile(Drive service, File file) { if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) { try { HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())) .execute(); return resp.getContent(); } catch (IOException e) { // An error occurred. e.printStackTrace(); return null; } } else { // The file doesn't have any content stored on Drive. return null; } }