docx4j and Google Drive
March 16th, 2014 by JasonGiven the news this week about Google lowering prices per GB on Google Drive, I thought it would be timely to explore interop with docx4j.
https://github.com/plutext/docx4j-cloud-GoogleDrive is a small project which demonstrates:
- Upload a wordMLPackage (or presentationML or spreadsheetML pkg) to Google Drive as a docx
- Download a file from Google Drive as a docx4j WordprocessingMLPackage
- Convert a WordprocessingMLPackage to the specified output format, using Google Drive
- press the red “CREATE NEW CLIENT ID” button, then choose application type “Installed Application”; I then chose subtype “Other”
- hit the “Download JSON” button; save it as client_secret.json in your project dir
Run our code
OK, now try running Docx4jUploadToGoogleDrive
It ought to say something like:
Please open the following URL in your browser then type the authorization code:https://accounts.google.com/o/oauth2/auth?access_type=online&client_id=622239…
Paste the auth code into your IDE’s console (System.in, probably the same place which displayed the above message) then press enter. If you aren’t logged into your Google account in your browser, its at this point that you’ll be asked to log in.
The code will create a new docx file, and after uploading it, if successful, report the File ID allocated by Google Drive:
File ID: 0CyHdofN18p16OF9YWWNFUFdmTjg
The other 2 samples require you to provide an auth code the same way (each time you run them). Obviously, you’d be more sophisticated than this in a production application. See further https://developers.google.com/drive/web/about-auth