Reputation: 4824
Using the Google Drive SDK (v2-rev57-1.13.2-beta) from Google, I am attempting to Export Google's proprietary formats.I am able to download non-proprietary formats just fine.
To get the path for the file, the documentation says to call:
com.google.api.services.drive.model.File.getExportLinks("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
to export (for example) a Google Document as a MS Word document. Doing this results in an apparently valid URL. (I've also tried this with "text/plain", with the same result).
However, when I attempt to download from that path, The Google Drive SDK throws a NullPointerException internally:
java.lang.NullPointerException
at com.google.api.client.googleapis.media.MediaHttpDownloader.download(MediaHttpDownloader.java:194)
at com.google.api.client.googleapis.media.MediaHttpDownloader.download(MediaHttpDownloader.java:150)
at com.redacted.mycode.GoogleDocsConnection.download(GoogleDocsConnection.java:182)
...
Is anybody else experiencing this? I'm considering modifying Google's SDK myself, but I wanted to see if anyone has already run into this problem (and maybe found a solution).
Thanks
Upvotes: 0
Views: 838
Reputation: 15004
UPDATED ANSWER:
I didn't read the initial question thoroughly and didn't notice that this happens only for documents in Google-native formats.
Once you have a valid export link for the document, you have to build an authorized GET request. The code to do that is similar to the one from the files.get
snippet below, you just need to replace the download url with the export url:
private static InputStream downloadFile(Drive service, String exportUrl) {
try {
HttpResponse resp =
service.getRequestFactory()
.buildGetRequest(new GenericUrl(exportUrl))
.execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
}
OLD ANSWER (please ignore):
The NullPointer exception might be caused by an unauthorized initial download request. The following code shows how to download a file from Drive:
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;
}
}
Just make sure the service
instance you are passing to downloadFile
is correctly authorized.
If you are using this code and you are sure your service is correctly authorized, please show the HTTP request sent by your app and the corresponding response.
Upvotes: 1