Skip to main content

An updated Dropbox API example

By far the most popular post on here (shock, I know!) was to do with using the Dropbox Java API from a desktop Java app. At the initial time of writing, this wasn't really documented, so the OAuth flow in particular required a bit of guesswork to get it going.

I haven't done any work with this API in quite a number of years now, and it appears the original v1 API was switched off a few months back, so that example will no longer work. This API has much better documentation than the v1 API did back in 2012, and if you're doing any serious work with it then looking through the SDK and the SDK examples on Github is a must.

That being said, for completeness I thought I'd provide an equivalent code snippet for the v2 API.

You'll need the appropriate Maven or Gradle dependency:

<dependency>
    <groupid>com.dropbox.core</groupid>
    <artifactid>dropbox-core-sdk</artifactid>
    <version>3.0.5</version>
</dependency>

or:

compile 'com.dropbox.core:dropbox-core-sdk:3.0.5'

If you so wish, you can still download the library and include it manually.

Then the following code snippet should just work similarly to before!

public class DropboxTest {

    //Create an app and get these details from https://www.dropbox.com/developers/apps
    private static final String APP_KEY = "APP KEY";
    private static final String APP_SECRET = "APP SECRET";

    public static void main(String[] args) throws Exception {

        DbxRequestConfig config = new DbxRequestConfig("berry120_dropbox_example"); //Client name can be whatever you like

        DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
        DbxWebAuth webAuth = new DbxWebAuth(config, appInfo);
        DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder()
                .withNoRedirect()
                .build();

        String url = webAuth.authorize(webAuthRequest);

        Desktop.getDesktop().browse(new URL(url).toURI());
        String code = JOptionPane.showInputDialog("Please click \"allow\" then enter your access code:");

        DbxAuthFinish authFinish = webAuth.finishFromCode(code);
        String accessToken = authFinish.getAccessToken(); //Store this for future use
        
        DbxClientV2 client = new DbxClientV2(config, accessToken);
        
        String fileContents = "Hello World!";
        ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContents.getBytes());
        client.files().uploadBuilder("/testing.txt").uploadAndFinish(inputStream);

    }
}


As before, when you've obtained the accessToken (by calling authFinish.getAccessToken()), you can then just store this and use it directly.

Comments

  1. Exception in thread "main" java.lang.IllegalArgumentException: String 'path' does not match pattern
    at com.dropbox.core.v2.files.CommitInfo$Builder.(CommitInfo.java:177)
    at com.dropbox.core.v2.files.CommitInfo.newBuilder(CommitInfo.java:158)
    at com.dropbox.core.v2.files.DbxUserFilesRequests.uploadBuilder(DbxUserFilesRequests.java:2521)
    at tapp.CloudApp.main(CloudApp.java:48)

    ReplyDelete
    Replies
    1. You'll get this exception if the path to the file you want to upload is invalid (I assume you've changed it from the default, as that definitely seems to work?)

      It has to start with a forward slash, and has to specify a filename (e.g. "/testing.txt" or "/myfiles/testing.txt".

      If you're still having issues then I'm afraid I'll need the exact code to help further!

      Delete
    2. Thanks Sir Michael! It's work

      Delete
    3. Do you want to help me how upload file to dropbox using JFileChooser, please?

      Exception in thread "main" com.dropbox.core.v2.files.UploadErrorException: Exception in 2/files/upload: {".tag":"path","reason":{".tag":"conflict","conflict":"folder"},"upload_session_id":"AAAAAAAAASPJhF6J15EJHA"}
      at com.dropbox.core.v2.files.UploadUploader.newException(UploadUploader.java:37)
      at com.dropbox.core.v2.files.UploadUploader.newException(UploadUploader.java:23)
      at com.dropbox.core.DbxUploader.finish(DbxUploader.java:225)
      at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:106)
      at com.dropbox.core.v2.DbxUploadStyleBuilder.uploadAndFinish(DbxUploadStyleBuilder.java:92)
      at tapp.CloudApp.main(CloudApp.java:60)

      Delete

Post a Comment

Popular posts from this blog

The comprehensive (and free) DVD / Blu-ray ripping Guide!

Note: If you've read this guide already (or when you've read it) then going through all of it each time you want to rip something can be a bit of a pain, especially when you just need your memory jogging on one particular section. Because of that, I've put together a quick "cheat sheet" here  which acts as a handy reference just to jog your memory on each key step. I've seen a few guides around on ripping DVDs, but fewer for Blu-rays, and many miss what I believe are important steps (such as ensuring the correct foreign language subtitles are preserved!) While ripping your entire DVD collection would have seemed insane due to storage requirements even a few years ago, these days it can make perfect sense. This guide doesn't show you a one click approach that does all the work for you, it's much more of a manual process. But the benefits of putting a bit more effort in really do pay off - you get to use entirely free tools with no demo versions, it&

Draggable and detachable tabs in JavaFX 2

JavaFX currently doesn't have the built in ability to change the order of tabs by dragging them, neither does it have the ability to detach tabs into separate windows (like a lot of browsers do these days.) There is a general issue for improving TabPanes filed here , so if you'd like to see this sort of behaviour added in the main JavaFX libraries then go ahead and cast your vote, it would be a very welcome addition! However, as nice as this would be in the future, it's not here at the moment and it looks highly unlikely it'll be here for Java 8 either. I've seen a few brief attempts at reordering tabs in JavaFX, but very few examples on dragging them and nothing to do with detaching / reattaching them from the pane. Given this, I've decided to create a reusable class that should hopefully be as easy as possible to integrate into existing applciations - it extends from Tab, and for the most part you create it and use it like a normal tab (you can just add it

Dropbox Java API

This code will no longer work! It uses the old v1 API, which has been turned off. See here for working code with the latest v2 API. As well as being useful as general cloud storage, dropbox also has an API that lets you access its contents programmatically. It's a straightforward REST API with a number of language specific libraries to make the going a bit easier. Java is included on the list of SDKs, but at present only Android is included on the list of tutorials. This can be somewhat frustrating because simple examples using Java are lacking. Fortunately, the process was described by Josh here . So I've taken it and implemented it in Java, and it seems to work. It's a very basic example that authenticates with dropbox then uploads a file called "testing.txt" containing "hello world." Of course, more functionality is available than this, but this is the hard part (at least I found working this bit out the hard part.) Once you've got your Dro