I'm writing a simple Dropbox program in Java that uploads files. Here is the particular piece of code that I am working with that creates a connection to a Dropbox account. The code below only runs the first time a user runs the app. After that the AccessTokenPair
is saved for use on subsequent runs.
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
WebAuthSession session = new WebAuthSession(appKeys, ACCESS_TYPE);
dropbox = new DropboxAPI<WebAuthSession>(session);
try {
if (!STATE_FILE.exists()) {
STATE_FILE.createNewFile();
WebAuthInfo authInfo = session.getAuthInfo();
RequestTokenPair pair = authInfo.requestTokenPair;
String url = authInfo.url;
URI confirmAddress = new URI(url);
Desktop desktop = Desktop.getDesktop();
desktop.browse(confirmAddress);
boolean authenticated = false;
while (!authenticated) {
try {
session.retrieveWebAccessToken(pair);
authenticated = true;
} catch (DropboxException dbe) {
io.tellUser("Waiting for user authentication...");
Thread.sleep(4000);
}
}
AccessTokenPair tokens = session.getAccessTokenPair();
State state = new State(STATE_FILE);
state.save(tokens);
It seems to work fine, but I am wary of the way I am checking for authentication. The code above will open a link in the user's default browser to his/her Dropbox account to allow access to this app. session.retrieveWebAccessToken(pair)
will throw the DropboxException
if access has not been allowed. So the program basically checks and catches the Exception then sleeps over and over until access has been granted. I would like to find a way to accomplish this without waiting and checking. I have tried a spin-lock/busy-lock and asking the user to press a button after they have authorized this app in addition to the sleep()
method implemented above. I would prefer to not ask the user for intervention. After reading some of the Dropbox API, I learned that you can use a callback url when constructing the WebAuthInfo
object, but I am not sure how to make that work because my app is not a web app. Does anyone have any idea how to use that callback for my purposes or any other ideas?