use of com.dropbox.core.DbxAuthFinish in project dropbox-sdk-java by dropbox.
the class Main method main.
public static void main(String[] args) throws IOException {
// Only display important log messages.
Logger.getLogger("").setLevel(Level.WARNING);
if (args.length != 2) {
System.out.println("Usage: COMMAND <app-info-file> <auth-file-output>");
System.out.println("");
System.out.println("<app-info-file>: a JSON file with information about your API app. Example:");
System.out.println("");
System.out.println(" {");
System.out.println(" \"key\": \"Your Dropbox API app key...\",");
System.out.println(" \"secret\": \"Your Dropbox API app secret...\"");
System.out.println(" }");
System.out.println("");
System.out.println(" Get an API app key by registering with Dropbox:");
System.out.println(" https://dropbox.com/developers/apps");
System.out.println("");
System.out.println("<auth-file-output>: If authorization is successful, the resulting API");
System.out.println(" access token will be saved to this file, which can then be used with");
System.out.println(" other example programs, such as the one in \"examples/account-info\".");
System.out.println("");
System.exit(1);
return;
}
String argAppInfoFile = args[0];
String argAuthFileOutput = args[1];
// Read app info file (contains app key and app secret)
DbxAppInfo appInfo;
try {
appInfo = DbxAppInfo.Reader.readFromFile(argAppInfoFile);
} catch (JsonReader.FileLoadException ex) {
System.err.println("Error reading <app-info-file>: " + ex.getMessage());
System.exit(1);
return;
}
// Run through Dropbox API authorization process
DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo);
DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder().withNoRedirect().build();
String authorizeUrl = webAuth.authorize(webAuthRequest);
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.");
System.out.print("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.finishFromCode(code);
} catch (DbxException ex) {
System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
System.exit(1);
return;
}
System.out.println("Authorization complete.");
System.out.println("- User ID: " + authFinish.getUserId());
System.out.println("- Access Token: " + authFinish.getAccessToken());
// Save auth information to output file.
DbxAuthInfo authInfo = new DbxAuthInfo(authFinish.getAccessToken(), appInfo.getHost());
File output = new File(argAuthFileOutput);
try {
DbxAuthInfo.Writer.writeToFile(authInfo, output);
System.out.println("Saved authorization information to \"" + output.getCanonicalPath() + "\".");
} catch (IOException ex) {
System.err.println("Error saving to <auth-file-out>: " + ex.getMessage());
System.err.println("Dumping to stderr instead:");
DbxAuthInfo.Writer.writeToStream(authInfo, System.err);
System.exit(1);
return;
}
}
use of com.dropbox.core.DbxAuthFinish in project dropbox-sdk-java by dropbox.
the class DropboxAuth method doFinish.
// -------------------------------------------------------------------------------------------
// GET /dropbox-auth-finish
// -------------------------------------------------------------------------------------------
// The Dropbox API authorization page will redirect the user's browser to this page.
//
// This is a GET (even though it modifies state) because we get here via a browser
// redirect (Dropbox redirects the user here). You can't do a browser redirect to
// an HTTP POST.
public void doFinish(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!common.checkGet(request, response))
return;
User user = common.requireLoggedInUser(request, response);
if (user == null) {
common.pageSoftError(response, "Can't do /dropbox-auth-finish. Nobody is logged in.");
return;
}
DbxAuthFinish authFinish;
try {
authFinish = getWebAuth(request).finishFromRedirect(getRedirectUri(request), getSessionStore(request), request.getParameterMap());
} catch (DbxWebAuth.BadRequestException e) {
common.log.println("On /dropbox-auth-finish: Bad request: " + e.getMessage());
response.sendError(400);
return;
} catch (DbxWebAuth.BadStateException e) {
// Send them back to the start of the auth flow.
response.sendRedirect(common.getUrl(request, "/dropbox-auth-start"));
return;
} catch (DbxWebAuth.CsrfException e) {
common.log.println("On /dropbox-auth-finish: CSRF mismatch: " + e.getMessage());
response.sendError(403);
return;
} catch (DbxWebAuth.NotApprovedException e) {
common.page(response, 200, "Not approved?", "Why not, bro?");
return;
} catch (DbxWebAuth.ProviderException e) {
common.log.println("On /dropbox-auth-finish: Auth failed: " + e.getMessage());
response.sendError(503, "Error communicating with Dropbox.");
return;
} catch (DbxException e) {
common.log.println("On /dropbox-auth-finish: Error getting token: " + e);
response.sendError(503, "Error communicating with Dropbox.");
return;
}
// We have an Dropbox API access token now. This is what will let us make Dropbox API
// calls. Save it in the database entry for the current user.
user.dropboxAccessToken = authFinish.getAccessToken();
common.saveUserDb();
response.sendRedirect("/");
}
Aggregations