Dropbox App example in Java CLI

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

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/

Create a dropbox app on dropbox.com to get your AppKey & AppSecretcreateDropBoxApp

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.

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.

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<WebAuthSession> sourceClient = new DropboxAPI<WebAuthSession>(session);

File file = new File("myFile.txt");
sourceClient.putFile("myFile.txt", new FileInputStream(file), file.length(), null, null);

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

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<WebAuthSession> sourceClient = new DropboxAPI<WebAuthSession>(session);

sourceClient.getFile("myFile.txt", null, new FileOutputStream( "myFile.txt"), null);

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

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

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

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

  • Health Verum

    Great tutorial really appreciate you taking the time to help others. Created my main walkthrough pretty similar but yet differently its more GUI based but still performs the basic principles with the URL and code retrieval.. Im having a hard time making my upload method with a JFileChooser to choose the file desired by the user and have been trying for a few days when I try to create your upload method that you have via. copy paste the AppPairKeys don’t seem to work at all I have tried working around this >? any ideas what Im doing wrong ?

    • Health Verum

      as in AppPAirKeys have red underlines and just gives the option ” Create Class” in the options when you hover over