New job — less time for my variations project. However I have other interesting topics on my mind which I’ll share with you.
This will be a post on the Dropbox API which I used to create a simple application to undelete files in your Dropbox folder. And I developed with Python and with Java.
In this article I’ll explain why did I use two languages — and how to develop a Dropbox application.
I got a simple task: create a Dropbox utility in Java to undelete some files from an account.
Why is this needed? If you use Dropbox you know that you can delete files — and you can undelete them. However you can restore each file only in single mode: you do not have an option to bulk-undelete. However with the provided APIs you can simple create an application which restores your deleted files.
The concrete task was to develop a Java application which can list or recover deleted files of a Dropbox user.
This is not bad, a simple application which uses the Dropbox API.
Creating an app for Dropbox
This is simple. To do this, you have to create an application at the Developers’ site of Dropbox (https://www.dropbox.com/developers/apps) and implement your application using the APIs provided by Dropbox.
To create an application, you have to answer some questions about what types of files your application needs (file types, only self-created or already existing in Dropbox, files or just data stores) and give your application a unique name (which mustn’t contain Dropbox or any related text) and you got a go.
After you created your application you have your app’s key and secret — which you need to call the API and create a session.
I won’t go into the details how to write an application using the provided APIs, how to authorise the users and co. There are a lot of tutorials and sample code (for example in the section “The code”).
However there are two major and important points of the applications created for Dropbox:
- Access: the access is needed by the users if you plan to contribute your application. If you do not have any plans to provide your tool to other users you can jump ahead to the next section. If you plan you have to change the preferences of your application. As you start to develop you are the only one who has the right to use the application with the provided key and secret. If you have some users you can click the “Enable additional users” to allow grants for up to 100 users (yourself inclusive). This is great isn’t it?
- More access: this comes after your application reached the 100 users. This happened to Tom Insam (who developed the Python tool for undeleting files). If you are done and want more users access you can click “Apply for production” at your application’s site. This does not mean that your application is instantly published. The Dropbox team will look at your application, evaluate it and than enable it (if not, you’ll be rejected). As for now my application stays in development mode. However if I see that more users need access I’ll apply for a finalised state.
But there is one problem with the Java API: there is no support for restoring or recovering files because they won’t be listed if you query the user’s folder. So I had to do the task in Python because it was urgent. There is already a similar implementation for the API (https://github.com/tominsam/dropbox-tools) but this version is not the same what I needed. So I forked the repository on GitHub and changed it according to my needs. The first need was to change the key and the secret of the application (see in the section “Creating an app for Dropbox”).
After this I could implement the requirements: have a specific argument list:
- <LIST or UNDELETE> for listing or recovering deleted files
- <from date> from which date to look up the deleted files
- <to date> when should the look up end (i.e. when was the last modification on the file, this is usually the deletion date)
- <recovery folder> the folder to copy the files to if the option UNDELETE is chosen
- <root folder> this is an optional parameter where you can specify from which folder the application should start to list or recover the deleted files
Because Ton Insam already did most of the coding I only had to apply my requirements to his code. The Python API has all the functions needed for this task so it was a small effort.
And as one extra surprise I’ve added a single executable (currently for Mac OS) so you do not have to install the oauth and dropbox Python libraries. The creation of this executable was as simple as in one previous article where I created an executable for Windows. Naturally I plan to add a Windows and Linux executable (if the current version does not already works with Linux).
However I’m not that kind who stops at such a problem that the API does not list deleted files. The Dropbox Java API is also on GitHub (https://github.com/dropbox/dropbox-sdk-java) so I forked it too and adapted it to my needs: I enabled querying for deleted files. I could have used the Python API too and call it from my Java code however I do not like such cross-references and perhaps it is better to change the Java API to do the functionality needed for the task.
After the API was finished I implemented the same application as in Python: a simple console app with required and optional arguments which does the same. And I added a single jar file for convenience.
Naturally there is one drawback: my Java API is not available in any maven repository. If you want to use it, you can download the code from my GitHub account and install it (mvn clean install) or download the JAR from the GitHub repository and add the archive to your maven repository with
<br /> mvn install:install-file -Dfile=dropbox-core-sdk.GHajba-1.jar -DgroupId=com.dropbox.core<br /> -DartifactId=dropbox-core-sdk -Dversion=GHajba-1 -Dpackaging=jar<br />
Perhaps in the future there will come a version of the dorpbox-sdk-java where you can query and modify deleted files too.
As an extra feature I added a single JAR creation with maven shade. And the application requires at least Java 7 to compile and run.
The code for this article is under my GitHub (as usual) however I included both the Python and the Java app in the same repository. The Python application is based on the forked version (mentioned in the Python section of this article) and the Java version has no ancestors. You find the repository here.