使用Google Drive API v2从Gdoc修订版导出PDF

时间:2017-04-16 19:37:38

标签: java google-drive-api

编辑:除了pdf之外的所有内容似乎都提供了正确的修订版。只有当我请求pdf时,无论我传递的修订版ID是什么,我都会得到最新版本

这应该很简单,但是当我尝试下载时,我总是得到头版本而不是以前的版本。

这是我的代码片段

RevisionList revList = service.revisions().list(fileId)
         .setMaxResults(10)
         .execute();
// Got all revisions here
List<Revision> revisions = revList.getItems();
if (revisions == null || revisions.size() == 0) {
    System.out.println("No revisions found.");
} else {
    // Got the first revision I made to the file
    Revision rev = revisions.get(0);
    // Date is in 2015 for this particular revision
    String date = rev.getModifiedDate().toString();
    String filePath = System.getProperty("user.dir") + "/" +
            date.substring(0, date.indexOf('T'))+".pdf";            
    OutputStream out = new FileOutputStream(filePath);
    MediaHttpDownloader downloader =
        new MediaHttpDownloader(HTTP_TRANSPORT, service.getRequestFactory().getInitializer());
    downloader.setProgressListener(new MediaHttpDownloaderProgressListener() {
        public void progressChanged(MediaHttpDownloader downloader) {
            switch (downloader.getDownloadState()) {
                case MEDIA_IN_PROGRESS:
                    System.out.println(downloader.getProgress());
                    break;
                case MEDIA_COMPLETE:
                    System.out.println("Download is complete!");
            }
        }
    });
    downloader.download(new GenericUrl(rev.getExportLinks().get("application/pdf")), out);
}

0 个答案:

没有答案