0

I am building an android app where the user have to download pictures from Dropbox. However every time, the user has to authenticate himself. I want the application to save the details first time and no authentication needed afterwards. The codes are below:

protected void initialize_session(){
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    mDBApi.getSession().startOAuth2Authentication(Control_Gate.this);
}


protected void 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);
        }
    }
    super.onResume();
}

This is for returning the user to the app. I know that the the solution must be in these two but I can't figure how to save the credentials.

2 Answers2

1

Save your accessToken in shared preference/SQLite.

for ex.

SharedPreferences sp = getSharedPreferences(
                                            "First_share_memory", Activity.MODE_APPEND);
                                    // save in cache memory
                                    sp.edit().putString("accesstoken", accessToken).commit();

and use this in getDropboxAPI method:

private DropboxAPI <AndroidAuthSession> getDropboxAPI() {
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);

SharedPreferences  sharedpreferences = getSharedPreferences("First_share_memory", Activity.MODE_APPEND);
String savedAccessToken = sharedpreferences.getString("accesstoken", "");// get previously saved accessToken

if (!TextUtils.isEmpty(savedAccessToken)) {
    mDBApi.getSession().setOAuth2AccessToken(savedAccessToken);
}

return mDBApi;
}

For more details see the ref:

link

Community
  • 1
  • 1
  • Thank you so much it worked. Just needed to include: else { mDBApi.getSession().startOAuth2Authentication(Control_Gate.this); } in the onResume method – Adarsh Ramlackhan Mar 16 '17 at 15:25
  • Another question i am passing through pubnub to send messages to the android application. I have written the codes in a service but now Android OS is killing the service. I have used START_STICKY also but it restarts the service several times and i miss some notification also. Its only 512MB ram. Is there another way where i can put the code for the mobile not to hang? – Adarsh Ramlackhan Mar 16 '17 at 15:29
  • Please create a new question.According to SO standard every question have a new thread.For detail U should read about background and foreground services. –  Mar 17 '17 at 05:07
  • See here for ref. http://stackoverflow.com/questions/42592897/create-a-android-app-service-which-run-even-after-app-is-terminated/42593015#42593015 –  Mar 17 '17 at 05:08
1

Save your token to SharedPrefernce and then use it accordingly. Below is the sample code for the same. Make following changes in your onResume function:

protected void onResume() {
        AndroidAuthSession session = mApi.getSession();
        setLoggedIn(mApi.getSession().authenticationSuccessful());
        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();
                storeKeys(tokens.key, tokens.secret);
                setLoggedIn(true);
            } catch (IllegalStateException e) {
                showToast(getString(R.string.could_not_authenticate_with_dropbox)
                        + e.getLocalizedMessage());
            }
        }
        super.onResume();
    }

Add storeKeys and clearKeys function to save values in SharedPreferences

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();
    }

    private void clearKeys() {
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        Editor edit = prefs.edit();
        edit.clear();
        edit.commit();
    }
private String[] getKeys() {
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        String key = prefs.getString(ACCESS_KEY_NAME, null);
        String secret = prefs.getString(ACCESS_SECRET_NAME, null);
        if (key != null && secret != null) {
            String[] ret = new String[2];
            ret[0] = key;
            ret[1] = secret;
            return ret;
        } else {
            return null;
        }
    }

And initialize your session like below:

public AndroidAuthSession buildSession() {
        AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
        AndroidAuthSession session;

        String[] stored = getKeys();
        if (stored != null) {
            AccessTokenPair accessToken = new AccessTokenPair(stored[0],
                    stored[1]);
            session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE,
                    accessToken);
        } else {
            session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
        }
        return session;
    }

Edit: Add these three constants and you can comment the call of setLoggedIn(true);

final static private String ACCOUNT_PREFS_NAME = "prefs";
final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
nnn
  • 980
  • 6
  • 13