Search in sources :

Example 1 with DbxWebAuth

use of com.dropbox.core.DbxWebAuth 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("- Account ID: " + authFinish.getAccountId());
    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 DbxWebAuth

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

the class ShortLiveTokenAuthorize method authorize.

public DbxAuthFinish authorize(DbxAppInfo appInfo) throws IOException {
    // Run through Dropbox API authorization process
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
    DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo);
    // TokenAccessType.OFFLINE means refresh_token + access_token. ONLINE means access_token only.
    DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder().withNoRedirect().withTokenAccessType(TokenAccessType.OFFLINE).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);
    }
    code = code.trim();
    try {
        return webAuth.finishFromCode(code);
    } catch (DbxException ex) {
        System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
        System.exit(1);
        return null;
    }
}
Also used : DbxRequestConfig(com.dropbox.core.DbxRequestConfig) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) DbxException(com.dropbox.core.DbxException) DbxWebAuth(com.dropbox.core.DbxWebAuth)

Example 3 with DbxWebAuth

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

the class ScopeAuthorize method authorize.

public DbxAuthFinish authorize(DbxAppInfo appInfo) throws IOException {
    // Run through Dropbox API authorization process
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
    DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo);
    // OAuth2 flow 1: Ask for account_info.read scope. Get a token with account_info.read
    DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder().withNoRedirect().withTokenAccessType(TokenAccessType.OFFLINE).withScope(Collections.singletonList("account_info.read")).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);
    }
    code = code.trim();
    try {
        DbxAuthFinish authFinish = webAuth.finishFromCode(code);
        assert authFinish.getScope().contains("account_info.read");
        if (!authFinish.getScope().contains("account_info.read")) {
            System.err.println("You app doesn't have account_info.read scope. Can't finish " + "this example.");
            System.exit(1);
            return null;
        }
        System.out.println("Successfully requested scope " + authFinish.getScope());
    } catch (DbxException ex) {
        System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
        System.exit(1);
        return null;
    }
    // OAuth2 flow 2: Ask for files.metadata.write only. Get a token with files.metadata
    // .write and its dependency files.metadata.read.
    webAuthRequest = DbxWebAuth.newRequestBuilder().withNoRedirect().withTokenAccessType(TokenAccessType.OFFLINE).withScope(Collections.singletonList("files.metadata.write")).build();
    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: ");
    code = new BufferedReader(new InputStreamReader(System.in)).readLine();
    if (code == null) {
        System.exit(1);
    }
    code = code.trim();
    try {
        DbxAuthFinish authFinish = webAuth.finishFromCode(code);
        if (!authFinish.getScope().contains("files.metadata.write")) {
            System.err.println("You app doesn't have files.metadata.write scope. Can't finish " + "this example.");
            System.exit(1);
            return null;
        }
        assert !authFinish.getScope().contains("account_info.read");
        System.out.println("Successfully requested scope " + authFinish.getScope());
    } catch (DbxException ex) {
        System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
        System.exit(1);
        return null;
    }
    // Oauth2 flow 3: Ask for "files.content.read", "files.content.write" and "sharing.read",
    // along with all previously granted scopes.
    webAuthRequest = DbxWebAuth.newRequestBuilder().withNoRedirect().withTokenAccessType(TokenAccessType.OFFLINE).withScope(Arrays.asList("files.content.read", "files.content.write", "sharing.read")).withIncludeGrantedScopes(IncludeGrantedScopes.USER).build();
    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: ");
    code = new BufferedReader(new InputStreamReader(System.in)).readLine();
    if (code == null) {
        System.exit(1);
    }
    code = code.trim();
    try {
        DbxAuthFinish authFinish = webAuth.finishFromCode(code);
        if (!authFinish.getScope().contains("files.content.read")) {
            System.err.println("You app doesn't have files.content.read scope. Can't finish " + "this example.");
            System.exit(1);
            return null;
        }
        if (!authFinish.getScope().contains("files.content.write")) {
            System.err.println("You app doesn't have files.content.write scope. Can't finish" + " " + "this example.");
            System.exit(1);
            return null;
        }
        if (!authFinish.getScope().contains("sharing.read")) {
            System.err.println("You app doesn't have sharing.read scope. Can't finish" + " " + "this example.");
            System.exit(1);
            return null;
        }
        assert authFinish.getScope().contains("account_info.read");
        assert authFinish.getScope().contains("files.metadata.write");
        assert authFinish.getScope().contains("sharing.read");
        System.out.println("Successfully requested scope " + authFinish.getScope());
        return authFinish;
    } catch (DbxException ex) {
        System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
        System.exit(1);
        return null;
    }
}
Also used : DbxRequestConfig(com.dropbox.core.DbxRequestConfig) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) DbxAuthFinish(com.dropbox.core.DbxAuthFinish) DbxException(com.dropbox.core.DbxException) DbxWebAuth(com.dropbox.core.DbxWebAuth)

Aggregations

DbxException (com.dropbox.core.DbxException)3 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)3 DbxWebAuth (com.dropbox.core.DbxWebAuth)3 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 DbxAuthFinish (com.dropbox.core.DbxAuthFinish)2 DbxAppInfo (com.dropbox.core.DbxAppInfo)1 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)1 JsonReader (com.dropbox.core.json.JsonReader)1 File (java.io.File)1 IOException (java.io.IOException)1