Search code examples
androidaccess-tokenaccountmanagerauthenticator

Retrieve Access token from Accounts Manager


I have implemented my own authenticator for Facebook and stored access_token in Accounts Manager.

I have tried all 3 ways to retrieve auth_token, but in vain.

In my method, I have started a thread, and in the Threads RUN method, I have tried foll ways:-

authTokenBundle = accountManagerFuture.getResult();

manager.blockingGetAuthToken(accounts[0], "com.facebook", false);

& the way u showed above...

manager.getAuthToken(account, "com.facebook", true, new AccountManagerCallback() {...

But my code gets blocked on all above 3 lines. No exception/error.

I need to get the auth-token in my app.


Solution

  • You can use the applications SharedPreferences to store and retreive the token.

    My implementation:

        private String getTokenFromSharedPreferences(){
            SharedPreferences settings = getSharedPreferences();
            return settings.getString(TOKEN, null);
        }
    
        private String getTokenSecretFromSharedPreferences(){
            SharedPreferences settings = getSharedPreferences();
            return settings.getString(TOKEN_SECRET, null);
        }
    
        public void setTokenInSharedPreferences(String token){
            putInSharedPreferences(TOKEN, token);
        }
    
        public void setTokenSecretInSharedPreferences(String tokenSecret){
            putInSharedPreferences(TOKEN_SECRET, tokenSecret);
        }
    
        public void putInSharedPreferences(String key, String value){
            SharedPreferences.Editor editor = getSharedPreferences().edit();
            editor.putString(key, value);
            editor.commit();
        }
    
        protected SharedPreferences getSharedPreferences(){
            return activity.getSharedPreferences(PREFERENCE_NAME, 0); //0 = MODE_PRIVATE.
        }