Search in sources :

Example 1 with DbxPKCEWebAuth

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

the class PkceAuthorize method authorize.

public DbxAuthFinish authorize(DbxAppInfo appInfo) throws IOException {
    // Run through Dropbox API authorization process without client secret
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
    DbxAppInfo appInfoWithoutSecret = new DbxAppInfo(appInfo.getKey());
    DbxPKCEWebAuth pkceWebAuth = new DbxPKCEWebAuth(requestConfig, appInfoWithoutSecret);
    DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder().withNoRedirect().withTokenAccessType(TokenAccessType.OFFLINE).build();
    String authorizeUrl = pkceWebAuth.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 {
        // exchange.
        return pkceWebAuth.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) DbxAppInfo(com.dropbox.core.DbxAppInfo) BufferedReader(java.io.BufferedReader) DbxPKCEWebAuth(com.dropbox.core.DbxPKCEWebAuth) DbxException(com.dropbox.core.DbxException) DbxWebAuth(com.dropbox.core.DbxWebAuth)

Aggregations

DbxAppInfo (com.dropbox.core.DbxAppInfo)1 DbxException (com.dropbox.core.DbxException)1 DbxPKCEWebAuth (com.dropbox.core.DbxPKCEWebAuth)1 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)1 DbxWebAuth (com.dropbox.core.DbxWebAuth)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1