Search in sources :

Example 1 with DbxOAuth1AccessToken

use of com.dropbox.core.DbxOAuth1AccessToken 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 2 with DbxOAuth1AccessToken

use of com.dropbox.core.DbxOAuth1AccessToken in project keepass2android by PhilippC.

the class DropboxV2Storage method onResume.

@Override
public void onResume(final FileStorageSetupActivity activity) {
    if (activity.getProcessName().equals(PROCESS_NAME_SELECTFILE))
        activity.getState().putString(EXTRA_PATH, activity.getPath());
    Log.d("KP2AJ", "OnResume (3). LoggedIn=" + mLoggedIn);
    /*if (mLoggedIn)
		{
			finishActivityWithSuccess(activity);
			return;
		}*/
    final String[] storedV1Keys = getKeysV1();
    if (storedV1Keys != null) {
        new AsyncTask<Object, Object, Object>() {

            @Override
            protected Object doInBackground(Object... objects) {
                DbxOAuth1AccessToken v1Token = new DbxOAuth1AccessToken(storedV1Keys[0], storedV1Keys[1]);
                DbxOAuth1Upgrader upgrader = new DbxOAuth1Upgrader(requestConfig, appInfo);
                try {
                    String v2Token = upgrader.createOAuth2AccessToken(v1Token);
                    upgrader.disableOAuth1AccessToken(v1Token);
                    storeKey(v2Token);
                    return v2Token;
                } catch (Exception e) {
                    e.printStackTrace();
                    clearKeys();
                    return null;
                }
            }

            @Override
            protected void onPostExecute(Object o) {
                if (o != null) {
                    buildSession();
                    finishActivityWithSuccess(activity);
                } else
                    resumeGetAuthToken(activity);
            }
        }.execute();
    } else {
        resumeGetAuthToken(activity);
    }
}
Also used : DbxOAuth1Upgrader(com.dropbox.core.DbxOAuth1Upgrader) DbxOAuth1AccessToken(com.dropbox.core.DbxOAuth1AccessToken) GetMetadataErrorException(com.dropbox.core.v2.files.GetMetadataErrorException) DownloadErrorException(com.dropbox.core.v2.files.DownloadErrorException) FileNotFoundException(java.io.FileNotFoundException) InvalidAccessTokenException(com.dropbox.core.InvalidAccessTokenException) DbxException(com.dropbox.core.DbxException) ListFolderErrorException(com.dropbox.core.v2.files.ListFolderErrorException) DeleteErrorException(com.dropbox.core.v2.files.DeleteErrorException)

Aggregations

DbxException (com.dropbox.core.DbxException)2 DbxOAuth1AccessToken (com.dropbox.core.DbxOAuth1AccessToken)2 DbxOAuth1Upgrader (com.dropbox.core.DbxOAuth1Upgrader)2 DbxAppInfo (com.dropbox.core.DbxAppInfo)1 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)1 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)1 InvalidAccessTokenException (com.dropbox.core.InvalidAccessTokenException)1 JsonReader (com.dropbox.core.json.JsonReader)1 DeleteErrorException (com.dropbox.core.v2.files.DeleteErrorException)1 DownloadErrorException (com.dropbox.core.v2.files.DownloadErrorException)1 GetMetadataErrorException (com.dropbox.core.v2.files.GetMetadataErrorException)1 ListFolderErrorException (com.dropbox.core.v2.files.ListFolderErrorException)1 FileNotFoundException (java.io.FileNotFoundException)1