Download file as pdf from Google Drive

不羁岁月 提交于 2020-01-06 02:25:07

问题


I have to download all Google drive files. i am trying to follow this for Getting download url and using this code for pdf url i am getting downloadurl using

file.getDownloadUrl() 

but when i am calling for download or converting into stream it is throwing error 404: not found.

where i am missing .. some authentication issue or some thing beyond ..... for downloading i am using like this ...

InputStream tempstream = Temp.downloadFile(service, file);

and

private static InputStream downloadFile(Drive service, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
  try {
      //download url is  : "https://doc-0s-9k-docs.googleusercontent.com/docs/securesc/6rpci9p5rgc1ards1udmrkv5oparm1a2/eoas3kt0uj6afg559kiugg3d6fftlere/1357740000000/06400000604443042302/18001816663358798513/0B2tdn0tkhMowTFVyRzYzaGhWeTQ?h=16653014193614665626&e=download&gd=true" 
    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;
}

}

downloadurl please help... i feel some i am not able to call method with proper authentication....


回答1:


You must make sure:

  1. Your service is authorized
  2. Your download URL is fresh (because they expire)

Otherwise your code is correct. For more info:

https://developers.google.com/drive/manage-downloads



来源:https://stackoverflow.com/questions/14232487/download-file-as-pdf-from-google-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!