Steve Weaver
Steve Weaver

Reputation: 350

Android Dropbox API - How to retrieve auth keys from SharedPreferences

I am using the Dropbox core Api in my app. When a user authorizes the app at launch, a key and a secret key are created which allows the app to interace with their dropbox. I am storing those keys in SharedPreferences by doing this:

private void storeKeys(String key, String secret) {
    // Save the access key for later
    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
    Editor edit = prefs.edit();
    edit.putString(ACCESS_KEY_NAME, key);
    edit.putString(ACCESS_SECRET_NAME, secret);
    edit.commit();
    Log.d("DbAuthLog", key);
    Log.d("DbAuthLog", secret);
}

The key and the secret are displayed in the log window as jkhfdsfueyuefd and yde767eyshy (fake keys for security of course).

Now, when I go to retrieve they keys, I am doing this:

private AccessTokenPair getStoredKeys() { //Need to fix this and test

    SharedPreferences accessKey = getSharedPreferences(ACCESS_KEY_NAME, 0);
    SharedPreferences secretKey = getSharedPreferences(ACCESS_SECRET_NAME, 0);
    System.out.println(accessKey + "--" + secretKey + "from storeKeys");
    return null;

The issue is, when I output the items from getStoredKeys, they are returned as --android.app.SharedPreferencesImpl@41201188--android.app.SharedPreferencesImpl@41201928 -- which in now way matches what I stored. What am I doing wrong here?

Upvotes: 2

Views: 870

Answers (2)

Yalla T.
Yalla T.

Reputation: 3727

You are Storing these tokens as Strings inside of a SharedPreference, you need to get them back as Strings.

private AccessTokenPair getStoredKeys() { //Need to fix this and test
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
String accessKey = prefs.getString(ACCESS_KEY_NAME, "");
String secretKey = prefs.getString(ACCESS_SECRET_NAME, "");
System.out.println(accessKey + "--" + secretKey + "from storeKeys");
return null;

Upvotes: 1

Justin Breitfeller
Justin Breitfeller

Reputation: 13801

You are accessing your preferences incorrectly.

SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
String accessKey = prefs.getString(ACCESS_KEY_NAME);
String secretKey = prefs.getString(ACCESS_SECRET_NAME);

Upvotes: 2

Related Questions