Skip to content

Instantly share code, notes, and snippets.

@badescunicu
Created March 21, 2014 20:14
Show Gist options
  • Save badescunicu/9695395 to your computer and use it in GitHub Desktop.
Save badescunicu/9695395 to your computer and use it in GitHub Desktop.
Dropbox's Java SDK file upload test
// Include the Dropbox SDK.
import com.dropbox.core.*;
import java.io.*;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws IOException, DbxException {
// The keys for my Dropbox test application
final String APP_KEY = "ja5m0h48oxetwr7";
final String APP_SECRET = "bgjlpji8n46lfgd";
DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
DbxRequestConfig config = new DbxRequestConfig("MyFirstApp/1.0",
Locale.getDefault().toString());
DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);
// Have the user sign in and authorize your app.
String authorizeUrl = webAuth.start();
System.out.println("1. Go to: " + authorizeUrl);
System.out.println("2. Click \"Allow\" (you might have to log in first)");
System.out.println("3. Copy the authorization code.");
String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
// This will fail if the user enters an invalid authorization code.
DbxAuthFinish authFinish = webAuth.finish(code);
String accessToken = authFinish.accessToken;
DbxClient client = new DbxClient(config, accessToken);
// Print the name of the current linked account
System.out.println("Linked account: " + client.getAccountInfo().displayName);
// Initialize the file to be uploaded
File inputFile = new File(args[0]);
FileInputStream inputStream = new FileInputStream(inputFile);
// Upload the file to root Dropbox directory
DbxEntry.File uploadedFile = client.uploadFile("/" + inputFile.getName(),
DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
inputStream.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment