Dropbox? On the EV3?
The first thing to realise is that I’m not going to show you how to install the Dropbox helper on your EV3. Instead we’re going to use the Dropbox Core Java SDK to connect our EV3 to your existing Dropbox account (free or pro, doesn’t matter) and then upload and download files into the account.
Dropbox is well known as a cloud file-sync service that ‘just works’. I’ve been using it for years, both as a pro and business user, and the software runs in the background, never complains and just gets on with the business of keeping all of your files in sync. Luckily Dropbox have extended the same simplicity to their SDK; I was amazing at just how easy it is to use the Java SDK in leJOs 0.9.0 to upload images captured from a webcam to my Dropbox account.
- You have a Dropbox account
- You have Eclipse installed and leJOS plugin installed
- You have a wifi equipped EV3 connected onto your home wifi
- You are comfortable logging into your EV3 using ssh in the terminal; you’ll need to run a program on the command line to authorise your EV3 to talk to Dropbox.
Step 1: Download the Dropbox Core API
Step 2: Create a new leJOS project in Eclipse
Step 3: Import the Dropbox Jar files from the SDK into your project
Step 4: Create a new Dropbox app
Step 5: Get the app key and app secret
Step 6: Copy the Dropbox JARs onto the EV3
Step 7: Authorise the EV3 to Dropbox, get the auth token
jrun -cp /home/lejos/programs/lib/dropbox-core-sdk-1.7.7.jar:/home/lejos/programs/lib/jackson-core-2.2.4.jar:GetAccessToken.jar GetAccessToken
Step 8: You’re done!
The code
Here's the code you need.
import com.dropbox.core.*;
import com.dropbox.core.json.JsonReader;
import java.io.*;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import lejos.hardware.lcd.LCD;
/**
* An example command-line application that runs through the web-based OAuth
* flow (using {@link DbxWebAuth}).
*
* By Mark Crosbie http://thinkbricks.net
* Modified from the Dropbox tutorial code
*/
public class GetAccessToken {
// The access token is saved in this file on the EV3 SDcard
public static final String accessTokenFilename = "./access-token.json";
public static void main(String[] args) throws IOException {
LCD.clear();
System.out.println("-- Get Dropbox auth token --");
System.out.println("1. Log into Dropbox and go to the App Console in your account");
System.out.println("2. Click on the app you created for the EV3");
System.out.print("3. Enter the App key value here: ");
String appKey = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (appKey == null) {
System.exit(1); return;
}
appKey = appKey.trim();
System.out.println("");
System.out.print("4. Enter the App secret value here: ");
String appSecret = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (appSecret == null) {
System.exit(1); return;
}
appSecret = appSecret.trim();
System.out.println("");
System.out.println("5. Starting Dropbox auth flow");
DbxAppInfo appInfo = new DbxAppInfo(appKey, appSecret);
String userLocale = Locale.getDefault().toString();
DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0", userLocale);
DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);
// Run through Dropbox API authorization process
String authorizeUrl = webAuth.start();
System.out.println("6. Go to " + authorizeUrl);
System.out.println("7. Click \"Allow\" (you might have to log in first).");
System.out.println("8. Copy the authorization code.");
System.out.print("9. Enter the authorization code here: ");
String code = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (code == null) {
System.exit(1); return;
}
code = code.trim();
DbxAuthFinish authFinish;
try {
authFinish = webAuth.finish(code);
}
catch (DbxException ex) {
System.err.println("Error in DbxWebAuth.start: " + ex.getMessage());
System.exit(1); return;
}
System.out.println("10. Authorization complete.");
System.out.println("- User ID: " + authFinish.userId);
System.out.println("- Access Token: " + authFinish.accessToken);
// Save auth information to output file.
DbxAuthInfo authInfo = new DbxAuthInfo(authFinish.accessToken, appInfo.host);
try {
DbxAuthInfo.Writer.writeToFile(authInfo, accessTokenFilename);
System.out.println("Saved authorization information to \"" + accessTokenFilename + "\".");
}
catch (IOException ex) {
System.err.println("Error saving to : " + ex.getMessage());
System.err.println("Dumping to stderr instead:");
DbxAuthInfo.Writer.writeToStream(authInfo, System.err);
System.exit(1); return;
}
}
}
Jan 11, 2016 @ 02:19:16
Thanks a lot have been searching for this code for a week got very frustrated I was trying to write it to a fileoutputsteam…. is there code to call the API for example when I try to upload a file to dropbox its not letting me after closing my app so there would be a api call for this code that you created?
Example
public void phoneHome (){
//open file saved in app (JSON)
auth information and allow user to upload the file without logging in the long way..