Let’s prototype a Dropbox Application who send file from you client to a server without using FTP.
We will look more in detail into JCommander library and how to authenticate and send file into your dropbox.
Recommended reading
•how to create an executable jar file
•Command line interface in java
Dropbox App: dir2server
In this tutorial we will create “dir2server†a small software able to send file from your computer (client) into your dropbox. Then an other computer (your server) can download it. Handy to exchange data between client and server without using FTP.
Dropbox authentification with Java
Before being able to write data into a dropbox folder you need to authenticate yourself as the owner of the dropbox.
For security raison dropbox don’t allow you to simply login with your login/password. You need to authenticate yourself to a Dropbox App who can then read/write into your folder.
You will need to get 4 keys to authenticate yourself:
AppKey & AppSecret – When creating the app
AccessKey & AccessSecret – When the app is granted for read/write
Creating the Dropbox App
•No need to follow this step, I already created the app. The owner of the app can’t do anything without your AccessKey & AccessSecret so don’t exchange those information with anyone.
•Dropbox apps are “lock†to there folder. They can’t read/write or remove any files outside of there directory.
In our case dir2server is lock into the directory /Dropbox/Apps/dir2server/
After clicking “create†you will see you App Keys display. In my case:
App key: o40z96h1ey82hoq
App secret: b6l1nbuk98xf5y2
Authenticate to Dropbox App in Java
Look inside the source code of dir2server and you will see the authentification process.
12345678910111213141516171819202122232425 public AccessTokenPair request() throws DropboxException, MalformedURLException, IOException, URISyntaxException { AppKeyPair appKeys = new AppKeyPair("YOUR_AppKey", "YOUR_AppSecret"); //AppKeyPair appKeys = new AppKeyPair("o40z96h1ey82hoq", "b6l1nbuk98xf5y2"); WebAuthSession session = new WebAuthSession(appKeys, AccessType.APP_FOLDER); WebAuthInfo authInfo = session.getAuthInfo(); RequestTokenPair pair = authInfo.requestTokenPair; String url = authInfo.url; //wait for user to process authentification on HTTPS System.out.println("Accept the app by openning your browser at this URL:"); System.out.println(url); System.out.println("THEN press any key:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br.readLine(); try { session.retrieveWebAccessToken(pair); } catch (Exception e) { System.out.println("authentication fail with exception:" + e); } AccessTokenPair tokens = session.getAccessTokenPair(); return tokens;}
As you can see the class WebAuthSession written by the Dropbox team take care of the hard work.
It’s sending back a HTPPS URL you need to connect to and grant the app.
In my case it was https://www.dropbox.com:443/1/oauth/authorize?oauth_token=RDrnZQsxx7povwhiB
Copy the URL from the console, open in your browser, grant the app THEN go back to your console and press any key.
In my case I get something like:
AccessKey q6ef0nl0hh_XXXXX_
AccessSecret rj9sopth_XXXXX_
Uploading into Dropbox in Java
With your 4 keys you can now build DropboxAPI object and use it to upload a file.
123456789 AppKeyPair appKeys = new AppKeyPair("YOUR_AppKey", "YOUR_AppSecret");//AppKeyPair appKeys = new AppKeyPair("o40z96h1ey82hoq", "b6l1nbuk98xf5y2");AccessTokenPair sourceAccess = new AccessTokenPair("YOUR_AccessKey","YOUR_AccessSecret"); WebAuthSession session = new WebAuthSession(pair,Session.AccessType.APP_FOLDER, sourceAccess);DropboxAPI
Again the Dropbox API help use a lot. We just need to rebuild a authicate web session and call the “putFile()†function to start the upload.
Downloading from Dropbox in Java
12345678 AppKeyPair appKeys = new AppKeyPair("YOUR_AppKey", "YOUR_AppSecret");//AppKeyPair appKeys = new AppKeyPair("o40z96h1ey82hoq", "b6l1nbuk98xf5y2");AccessTokenPair sourceAccess = new AccessTokenPair("YOUR_AccessKey","YOUR_AccessSecret"); WebAuthSession session = new WebAuthSession(pair,Session.AccessType.APP_FOLDER, sourceAccess);DropboxAPI
Same idea as uploading file, the “getFile()†fonction do all the hard work.
Control dir2server using the CLI
Looking at the entry point of our java prototype
12345678910 public static void main(String[] args){ Main main = new Main(); Client2ServerCmd cmd = new Client2ServerCmd(); new JCommander(cmd, args); main.mightSaveTokenCmd(cmd); main.mightUploadCmd(cmd); main.mightDownloadCmd(cmd);}
Our object Client2ServerCmd is feeded with the matching command line using the library JCommander
1234567 public class Client2ServerCmd { /* download */ @Parameter(names = "--download", description = "File to download") private String downloadFile; [...]}
main.mightUploadCmd(cmd); will check if the command is valid for upload. If it is, then the upload to dropbox function will be call.
When you export into an executable jar file you can send command from you command line interface
1 java -jar dir2server.jar --upload myFile.txt
dir2server do more!
With dir2server you can start downloading a file before it finish to upload or even started!
Also you can get an email notification when the download finish
Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology