after reading the official Dropbox documentation, I managed to write this code to authenticate the user with DropBox and get his access token. The user has to copy and paste the token, I don't like this step and I noticed that some developer can use the withRedirect()
method of the DbxWebAuth
class.
There is an example for using redirect, but it is for web applications and I was unable to adapt it to my desktop app. Have any of you had anything to do with this?
This is currently my code
public static void main(String[] args) throws Exception {
String accessToken = "";
String userLocale = null;
DbxRequestConfig requestConfig = new DbxRequestConfig("text-edit/0.1", userLocale);
DbxAppInfo appInfo = new DbxAppInfo("myString", "myString");
DbxWebAuth auth = new DbxWebAuth(requestConfig, appInfo);
DbxWebAuth.Request requestAuth = DbxWebAuth.newRequestBuilder().withNoRedirect().build();
String authorizeUrl = auth.authorize(requestAuth);
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.");
//Abrimos el enlace de autenticación del paciente en la carpeta de DropBox
try {
URL authenticationURL = new URL(authorizeUrl);
Desktop.getDesktop().browse(authenticationURL.toURI());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JFrame frame1 = new JFrame("InputDialog Example #2");
frame1.setAlwaysOnTop(true);
String code = JOptionPane.showInputDialog(frame1, "Insert verification code");
System.out.println(code);
code = code.trim();
try {
DbxAuthFinish authFinish = auth.finishFromCode(code);
accessToken = authFinish.getAccessToken();
} catch (Exception e) {
}
}