Search in sources :

Example 1 with DbxAuthInfo

use of com.dropbox.core.DbxAuthInfo 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 DbxAuthInfo

use of com.dropbox.core.DbxAuthInfo 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("");
        System.out.println("Usage: COMMAND <auth-file> <dropbox-path>");
        System.out.println("");
        System.out.println(" <auth-file>: An \"auth file\" that contains the information necessary to make");
        System.out.println("    an authorized Dropbox API request.  Generate this file using the \"authorize\"");
        System.out.println("    example program.");
        System.out.println("");
        System.out.println(" <dropbox-path>: The path on Dropbox to watch for changes.");
        System.out.println("");
        System.exit(1);
        return;
    }
    String authFile = args[0];
    String path = args[1];
    // Read auth info file.
    DbxAuthInfo auth;
    try {
        auth = DbxAuthInfo.Reader.readFromFile(authFile);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error loading <auth-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    longpoll(auth, path);
}
Also used : DbxAuthInfo(com.dropbox.core.DbxAuthInfo) JsonReader(com.dropbox.core.json.JsonReader)

Example 3 with DbxAuthInfo

use of com.dropbox.core.DbxAuthInfo 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 4 with DbxAuthInfo

use of com.dropbox.core.DbxAuthInfo 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 != 1) {
        System.out.println("");
        System.out.println("Usage: COMMAND <auth-file>");
        System.out.println("");
        System.out.println(" <auth-file>: An \"auth file\" that contains the information necessary to make");
        System.out.println("    an authorized Dropbox API request.  Generate this file using the \"authorize\"");
        System.out.println("    example program.");
        System.out.println("");
        System.exit(1);
        return;
    }
    String argAuthFile = args[0];
    // Read auth info file.
    DbxAuthInfo authInfo;
    try {
        authInfo = DbxAuthInfo.Reader.readFromFile(argAuthFile);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error loading <auth-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    // Create a DbxClientV1, which is what you use to make API calls.
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-account-info");
    DbxClientV2 dbxClient = new DbxClientV2(requestConfig, authInfo.getAccessToken(), authInfo.getHost());
    // Make the /account/info API call.
    FullAccount dbxAccountInfo;
    try {
        dbxAccountInfo = dbxClient.users().getCurrentAccount();
    } catch (DbxException ex) {
        System.err.println("Error making API call: " + ex.getMessage());
        System.exit(1);
        return;
    }
    System.out.print(dbxAccountInfo.toStringMultiline());
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) JsonReader(com.dropbox.core.json.JsonReader) FullAccount(com.dropbox.core.v2.users.FullAccount) DbxException(com.dropbox.core.DbxException)

Example 5 with DbxAuthInfo

use of com.dropbox.core.DbxAuthInfo 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 != 3) {
        System.out.println("");
        System.out.println("Usage: COMMAND <auth-file> <local-path> <dropbox-path>");
        System.out.println("");
        System.out.println(" <auth-file>: An \"auth file\" that contains the information necessary to make");
        System.out.println("    an authorized Dropbox API request.  Generate this file using the \"authorize\"");
        System.out.println("    example program.");
        System.out.println("");
        System.out.println(" <local-path>: The path to a local file whose contents you want to upload.");
        System.out.println("");
        System.out.println(" <dropbox-path>: The path on Dropbox to save the file to.");
        System.out.println("");
        System.exit(1);
        return;
    }
    String argAuthFile = args[0];
    String localPath = args[1];
    String dropboxPath = args[2];
    // Read auth info file.
    DbxAuthInfo authInfo;
    try {
        authInfo = DbxAuthInfo.Reader.readFromFile(argAuthFile);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error loading <auth-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    String pathError = DbxPathV2.findError(dropboxPath);
    if (pathError != null) {
        System.err.println("Invalid <dropbox-path>: " + pathError);
        System.exit(1);
        return;
    }
    File localFile = new File(localPath);
    if (!localFile.exists()) {
        System.err.println("Invalid <local-path>: file does not exist.");
        System.exit(1);
        return;
    }
    if (!localFile.isFile()) {
        System.err.println("Invalid <local-path>: not a file.");
        System.exit(1);
        return;
    }
    // Create a DbxClientV2, which is what you use to make API calls.
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-upload-file");
    DbxClientV2 dbxClient = new DbxClientV2(requestConfig, authInfo.getAccessToken(), authInfo.getHost());
    // deciding factor. This should really depend on your network.
    if (localFile.length() <= (2 * CHUNKED_UPLOAD_CHUNK_SIZE)) {
        uploadFile(dbxClient, localFile, dropboxPath);
    } else {
        chunkedUploadFile(dbxClient, localFile, dropboxPath);
    }
    System.exit(0);
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) JsonReader(com.dropbox.core.json.JsonReader) File(java.io.File)

Aggregations

DbxAuthInfo (com.dropbox.core.DbxAuthInfo)6 JsonReader (com.dropbox.core.json.JsonReader)6 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)5 DbxException (com.dropbox.core.DbxException)3 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)3 DbxAppInfo (com.dropbox.core.DbxAppInfo)2 File (java.io.File)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 FullAccount (com.dropbox.core.v2.users.FullAccount)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1