2

I am trying to connect my Android 4+ to Dropbox. I am using the latest version of the Core API provided by Dropbox.

Everything works fine until I try to save the user key and token when the user returns to my app after he authenticated the access using dropboxAPI.getSession().startOAuth2Authentication(activity).

When the user returns to my app after the authentication the following code should save key and token:

public boolean completeLogInAfterAuthentication() {
    AndroidAuthSession session = dropboxAPI.getSession();
    if (session.authenticationSuccessful()) {
        try {
            // Mandatory call to complete the auth
            session.finishAuthentication();

            // Store it locally in our app for later use
            TokenPair tokens = session.getAccessTokenPair();
            saveSessionKeys(tokens.key, tokens.secret);
            return true;
        } catch (IllegalStateExceptione) {
            Log.d("MyLog", "Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
            Log.d("MyLog", "Error authenticating", e);
        }
    }
    return false;
}

This is quite exactly the code that is used in the DBRoulett Demo provided by Dropbox. Problem is, that session.getAccessTokenPair() returns null.

Because of this I cannot store any key or token and the user has to re-login everytime the app is started. How can I solve this?

All information I found just say, that getAccessTokenPair() could fail with an IllegalStateException but this is not the case here. The case that null is returned is not described anywhere. Any idea what I can do?

1 Answer 1

3

getAccessTokenPair is used to get an OAuth 1 access token (and secret). But you used OAuth 2, so you want getOAuth2AccessToken. From the tutorial (https://www.dropbox.com/developers/core/start/android):

protected void onResume() {
    super.onResume();

    if (mDBApi.getSession().authenticationSuccessful()) {
        try {
            // Required to complete auth, sets the access token on the session
            mDBApi.getSession().finishAuthentication();

            String accessToken = mDBApi.getSession().getOAuth2AccessToken();
        } catch (IllegalStateException e) {
            Log.i("DbAuthLog", "Error authenticating", e);
        }
    }
}

This is roughly the same thing that's in the DBRoulette sample, though it has code for both OAuth 1 and OAuth 2:

private void storeAuth(AndroidAuthSession session) {
    // Store the OAuth 2 access token, if there is one.
    String oauth2AccessToken = session.getOAuth2AccessToken();
    if (oauth2AccessToken != null) {
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        Editor edit = prefs.edit();
        edit.putString(ACCESS_KEY_NAME, "oauth2:");
        edit.putString(ACCESS_SECRET_NAME, oauth2AccessToken);
        edit.commit();
        return;
    }
    // Store the OAuth 1 access token, if there is one.  This is only necessary if
    // you're still using OAuth 1.
    AccessTokenPair oauth1AccessToken = session.getAccessTokenPair();
    if (oauth1AccessToken != null) {
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        Editor edit = prefs.edit();
        edit.putString(ACCESS_KEY_NAME, oauth1AccessToken.key);
        edit.putString(ACCESS_SECRET_NAME, oauth1AccessToken.secret);
        edit.commit();
        return;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. It seems that I have been using an outdated version of the DBRoulett sample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.