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

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...

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...

Using VLCJ for video reliably with out of process players

This post is really a follow on to the last, so if you want a bit of background give that a read through. In it I basically talk about the various options I came to when trying to implement video support in Java, and how I felt the best way to go was using out of process players and VLCJ. Before I get to the code, a warning - while this isn't stupidly hard, it's definitely not for beginners. If you're competent with Java in general you shouldn't have any trouble following - but this is not a cut / paste / forget the advanced stuff job! Eventually and depending on interest I may look into packaging it up into an easy to use library and distributing, but that day is not today! Secondly, this code is literally cut straight out of Quelea and at the moment is: Largely uncommented From an unreleased version Not the best quality May contain Quelea specific bits that don't apply Eventually, it'll all be refactored into beautiful OO-like niceness. But in terms of the con...