Load, save and use of authentication data in Box Android API

前端 未结 2 870
借酒劲吻你
借酒劲吻你 2021-01-13 18:20

I\'ve been recently trying to implement Box in my Android app. I know how to launch the authentication activity and get BoxAndroidClient object ready to operate on it, but I

2条回答
  •  借酒劲吻你
    2021-01-13 18:48

    This is my way to handle this find below my PreferencesUtil class.

    package com.omt.omtboxapi;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    
    import com.box.boxjavalibv2.dao.BoxOAuthToken;
    
    package com.omt.omtboxapi;
    
    public class PreferencesUtil {
    
    private static PreferencesUtil preferencesUtil;
    
    private PreferencesUtil() {
    
    }
    
    public static PreferencesUtil getInstance() {
    
        if (preferencesUtil == null) {
    
            synchronized (PreferencesUtil.class) {
    
                if (preferencesUtil == null) {
                    preferencesUtil = new PreferencesUtil();
                }
    
            }
    
        }
    
        return preferencesUtil;
    }
    
    public BoxOAuthToken getAuthToken(Context context) {
        BoxOAuthToken authToken = null;
        SharedPreferences preferences = context.getSharedPreferences(
                "OMT_BOX_SHARED", Context.MODE_PRIVATE);
        if (!preferences.getBoolean("IS_NEW", true)) {
    
            Map map = new HashMap();
    
            map.put(BoxOAuthToken.FIELD_ACCESS_TOKEN,
                    preferences.getString(BoxOAuthToken.FIELD_ACCESS_TOKEN, ""));
            map.put(BoxOAuthToken.FIELD_REFRESH_TOKEN, preferences.getString(
                    BoxOAuthToken.FIELD_REFRESH_TOKEN, ""));
            map.put(BoxOAuthToken.FIELD_TOKEN_TYPE,
                    preferences.getString(BoxOAuthToken.FIELD_TOKEN_TYPE, ""));
            map.put(BoxOAuthToken.FIELD_EXPIRES_IN,
                    preferences.getInt(BoxOAuthToken.FIELD_EXPIRES_IN, 0));
            authToken = new BoxOAuthToken(map);
        }
        return authToken;
    }
    
    public void saveAuthToken(BoxOAuthToken authToken, Context context) {
        SharedPreferences preferences = context.getSharedPreferences(
                "OMT_BOX_SHARED", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("IS_NEW", false);
        editor.putString(BoxOAuthToken.FIELD_ACCESS_TOKEN,
                authToken.getAccessToken());
        editor.putString(BoxOAuthToken.FIELD_REFRESH_TOKEN,
                authToken.getRefreshToken());
        editor.putString(BoxOAuthToken.FIELD_TOKEN_TYPE,
                authToken.getTokenType());
        editor.putInt(BoxOAuthToken.FIELD_EXPIRES_IN, authToken.getExpiresIn());
        editor.commit();
    }
    

    }

    Now to handle Refresh things below is my approach :

            client.addOAuthRefreshListener(new OAuthRefreshListener() {
    
                @Override
                public void onRefresh(IAuthData newAuthData) {
                    PreferencesUtil.getInstance().saveAuthToken(
                            (BoxOAuthToken) newAuthData, MainActivity.this);
                }
            });
    

    NOTE :

    The Keys that I am using in Preference is available in BoxOAuthToken so DO NOT CHANGE IT OTHERWISE YOUR CODE WILL NOT WORK..

    THOSE KEYS ARE :

    public static final String FIELD_ACCESS_TOKEN = "access_token";
    public static final String FIELD_EXPIRES_IN = "expires_in";
    public static final String FIELD_TOKEN_TYPE = "token_type";
    public static final String FIELD_REFRESH_TOKEN = "refresh_token";
    

提交回复
热议问题