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:
or:
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!
As before, when you've obtained the accessToken (by calling
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.
Exception in thread "main" java.lang.IllegalArgumentException: String 'path' does not match pattern
ReplyDeleteat 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)
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?)
DeleteIt 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!
Thanks Sir Michael! It's work
DeleteAwesome! Happy to help.
DeleteDo you want to help me how upload file to dropbox using JFileChooser, please?
DeleteException 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)