Search in sources :

Example 1 with DbxAppInfo

use of com.dropbox.core.DbxAppInfo 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;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) DbxAppInfo(com.dropbox.core.DbxAppInfo) BufferedReader(java.io.BufferedReader) JsonReader(com.dropbox.core.json.JsonReader) DbxAuthFinish(com.dropbox.core.DbxAuthFinish) File(java.io.File) DbxException(com.dropbox.core.DbxException) DbxWebAuth(com.dropbox.core.DbxWebAuth)

Example 2 with DbxAppInfo

use of com.dropbox.core.DbxAppInfo 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.INFO);
    Config cfg = parseArgs(args);
    if (cfg == null) {
        System.exit(1);
        return;
    }
    // Read app info file (contains app key and app secret)
    DbxAppInfo appInfo;
    try {
        appInfo = DbxAppInfo.Reader.readFromFile(cfg.appInfoFile);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error reading <app-info-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    DbxOAuth1AccessToken oauth1AccessToken = new DbxOAuth1AccessToken(cfg.accessTokenKey, cfg.accessTokenSecret);
    // Get an OAuth 2 access token.
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
    DbxOAuth1Upgrader upgrader = new DbxOAuth1Upgrader(requestConfig, appInfo);
    String oauth2AccessToken;
    try {
        oauth2AccessToken = upgrader.createOAuth2AccessToken(oauth1AccessToken);
    } catch (DbxException ex) {
        System.err.println("Error getting OAuth 2 access token: " + ex.getMessage());
        System.exit(1);
        return;
    }
    System.out.println("OAuth 2 access token obtained.");
    DbxAuthInfo authInfo = new DbxAuthInfo(oauth2AccessToken, appInfo.getHost());
    DbxAuthInfo.Writer.writeToStream(authInfo, System.out);
    System.out.println();
    // Disable the OAuth 1 access token.
    if (cfg.disable) {
        try {
            upgrader.disableOAuth1AccessToken(oauth1AccessToken);
        } catch (DbxException ex) {
            System.err.println("Error disabling OAuth 1 access token: " + ex.getMessage());
            System.exit(1);
            return;
        }
        System.out.println("OAuth 1 access token disabled.");
    }
}
Also used : DbxOAuth1Upgrader(com.dropbox.core.DbxOAuth1Upgrader) DbxOAuth1AccessToken(com.dropbox.core.DbxOAuth1AccessToken) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAppInfo(com.dropbox.core.DbxAppInfo) JsonReader(com.dropbox.core.json.JsonReader) DbxException(com.dropbox.core.DbxException)

Example 3 with DbxAppInfo

use of com.dropbox.core.DbxAppInfo in project dropbox-sdk-java by dropbox.

the class Main method main.

// -------------------------------------------------------------------------------------------
// main()
// -------------------------------------------------------------------------------------------
// Parse command-line arguments and start server.
public static void main(String[] args) throws IOException {
    if (args.length != 3) {
        System.out.println("");
        System.out.println("Usage: COMMAND <http-listen-port> <app-info-file> <database-file>");
        System.out.println("");
        System.out.println(" <http-listen-port>: The port to run the HTTP server on.  For example,");
        System.out.println("    \"8080\").");
        System.out.println("");
        System.out.println(" <app-info-file>: A JSON file containing your Dropbox API app key, secret");
        System.out.println("    and access type.  For example, \"my-app.app\" with:");
        System.out.println("");
        System.out.println("    {");
        System.out.println("      \"key\": \"Your Dropbox app key...\",");
        System.out.println("      \"secret\": \"Your Dropbox app secret...\"");
        System.out.println("    }");
        System.out.println("");
        System.out.println(" <database-file>: Where you want this program to store its database.  For");
        System.out.println("    example, \"web-file-browser.db\".");
        System.out.println("");
        System.exit(1);
        return;
    }
    String argPort = args[0];
    String argAppInfo = args[1];
    String argDatabase = args[2];
    // Figure out what port to listen on.
    int port;
    try {
        port = Integer.parseInt(argPort);
        if (port < 1 || port > 65535) {
            System.err.println("Expecting <http-listen-port> to be a number from 1 to 65535.  Got: " + port + ".");
            System.exit(1);
            return;
        }
    } catch (NumberFormatException ex) {
        System.err.println("Expecting <http-listen-port> to be a number from 1 to 65535.  Got: " + jq(argPort) + ".");
        System.exit(1);
        return;
    }
    // Read app info file (contains app key and app secret)
    DbxAppInfo dbxAppInfo;
    try {
        dbxAppInfo = DbxAppInfo.Reader.readFromFile(argAppInfo);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error loading <app-info-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    System.out.println("Loaded app info from " + jq(argAppInfo));
    File dbFile = new File(argDatabase);
    // Run server
    try {
        Main main = new Main(new PrintWriter(System.out, true), dbxAppInfo, dbFile);
        Server server = new Server(port);
        SessionHandler sessionHandler = new SessionHandler();
        sessionHandler.setServer(server);
        sessionHandler.setHandler(main);
        server.setHandler(sessionHandler);
        server.start();
        System.out.println("Server running: http://localhost:" + port + "/");
        server.join();
    } catch (Exception ex) {
        System.err.println("Error running server: " + ex.getMessage());
        System.exit(1);
    }
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) Server(org.eclipse.jetty.server.Server) DbxAppInfo(com.dropbox.core.DbxAppInfo) JsonReader(com.dropbox.core.json.JsonReader) File(java.io.File) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

DbxAppInfo (com.dropbox.core.DbxAppInfo)3 JsonReader (com.dropbox.core.json.JsonReader)3 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)2 DbxException (com.dropbox.core.DbxException)2 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)2 File (java.io.File)2 IOException (java.io.IOException)2 DbxAuthFinish (com.dropbox.core.DbxAuthFinish)1 DbxOAuth1AccessToken (com.dropbox.core.DbxOAuth1AccessToken)1 DbxOAuth1Upgrader (com.dropbox.core.DbxOAuth1Upgrader)1 DbxWebAuth (com.dropbox.core.DbxWebAuth)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 ServletException (javax.servlet.ServletException)1 Server (org.eclipse.jetty.server.Server)1 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)1