سؤال

I'm able to get authenticated using the Android SDK, but one issue I'm having (similar to this) is that the access token never refreshes after expiry, even though my refresh token hasn't expired yet. This is the scenario I'm working in:

  1. Prompt for user authentication. Upon success, store authdata as json string.

  2. Exit app. One hour later (access token expires), launch app.

  3. My app will check for previously saved tokens, and in this case finds one. It then pulls the old authdata and calls client.authenticate(authdata). Note: no errors at this point.

  4. At this point, I make API calls with the client, but get an error saying access token is invalid. An AuthFailure exception is also thrown with error: "Invalid grant", description: "Refresh token has expired". Not sure why this is the case, because Refresh token should not expire within the same day.

I've checked the token that was stored and compared to the one retrieved, no issues. Below is some code snippets.

Box init:

//check for previous tokens
    SharedPreferences settings = appContext.getSharedPreferences(PREFS_NAME, 0);
    if (settings.contains(PREFS_AUTHDATA)) {
        isLinked = true;
        try {
            String authToken = settings.getString(PREFS_AUTHDATA, "");
            Log.v(LOG_TAG, "Token:\n" + authToken);
            BoxAndroidOAuthData authData = new BoxJSONParser(new AndroidBoxResourceHub()).parseIntoBoxObject(authToken, BoxAndroidOAuthData.class);
            Log.v(LOG_TAG, "check access: " + authData.getAccessToken() + ", refresh: " + authData.getRefreshToken());

            mClient.addOAuthRefreshListener(new OAuthRefreshListener() {

                @Override
                public void onRefresh(IAuthData newAuthData) {
                    //save token
                    try {
                        String authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(newAuthData);

                        //save token
                        SharedPreferences settings = appContext.getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString(PREFS_AUTHDATA, authToken);
                        editor.commit();
                        Log.v(LOG_TAG, "Tokens refreshed and updated");
                    } catch (BoxJSONException e) {
                        Log.e(LOG_TAG, "Authentication error - BoxJSONException");
                        e.printStackTrace();
                    } catch (IOException e) {
                        Log.e(LOG_TAG, "Authentication error - IOException");
                        e.printStackTrace();
                    }
                }
            });
            mClient.authenticate(authData);

            Log.v(LOG_TAG, "Authenticated with previously stored tokens");
        } catch (BoxJSONException e) {
            Log.e(LOG_TAG, "Authentication error - BoxJSONException");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(LOG_TAG, "Authentication error - IOException");
            e.printStackTrace();
        }
    }

On Authenticate:

Log.v(LOG_TAG, "Starting Authentication");
    mCallback = (CloudSetupWebViewActivity)activity;
    webview.initializeAuthFlow(activity, CLIENT_ID, CLIENT_SECRET);
    mClient.authenticate(webview, true, new OAuthWebViewListener() {

        @Override
        public void onSslError(SslErrorHandler handler, SslError error) {
            Log.e(LOG_TAG, "Authentication error - onSslError: " + error.toString());
            handler.proceed();
        }

        @Override
        public void onError(int errorCode, String description, String failingUrl) {
            Log.e(LOG_TAG, "Authentication error - onError: " + description);
        }

        @Override
        public void onAuthFlowMessage(IAuthFlowMessage message) {
        }

        @Override
        public void onAuthFlowException(Exception e) {
            Log.e(LOG_TAG, "Authentication error - onAuthFlowException: " + e.getClass().getCanonicalName());
        }

        @Override
        public void onAuthFlowEvent(IAuthEvent event, IAuthFlowMessage message) {
            if (event == OAuthEvent.OAUTH_CREATED) {
                try {
                    OAuthDataMessage msg = (OAuthDataMessage) message;
                    String authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(msg.getData());
                    Log.v(LOG_TAG, "authToken: " + authToken);

                    //save token
                    SharedPreferences settings = appContext.getSharedPreferences(PREFS_NAME, 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString(PREFS_AUTHDATA, authToken);
                    editor.commit();
                    isLinked = true;
                    Log.v(LOG_TAG, "Authenticated and saved access/refresh token");
                    mCallback.onAuthenticationComplete();
                } catch (BoxJSONException e) {
                    Log.e(LOG_TAG, "Authentication error - BoxJSONException");
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.e(LOG_TAG, "Authentication error - IOException");
                    e.printStackTrace();
                }
            }

        }
    });

    mClient.addOAuthRefreshListener(new OAuthRefreshListener() {

        @Override
        public void onRefresh(IAuthData newAuthData) {
            //save token
            try {
                String authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(newAuthData);

                //save token
                SharedPreferences settings = appContext.getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString(PREFS_AUTHDATA, authToken);
                editor.commit();
                Log.v(LOG_TAG, "Tokens refreshed and updated");
            } catch (BoxJSONException e) {
                Log.e(LOG_TAG, "Authentication error - BoxJSONException");
                e.printStackTrace();
            } catch (IOException e) {
                Log.e(LOG_TAG, "Authentication error - IOException");
                e.printStackTrace();
            }
        }
    });

Thanks in advance!

هل كانت مفيدة؟

المحلول

after refreshing your oauth data , i think the oauth data is refreshed but you are not able to use it .

try putting this code after refreshing oauth data

  String authToken = settings.getString(PREFS_AUTHDATA, ""); 
  Log.v(LOG_TAG, "Token:\n" + authToken); 
  BoxAndroidOAuthData authData = newBoxJSONParser(newAndroidBoxResourceHub()).parseIntoBoxObject(authToken, BoxAndroidOAuthData.class); Log.v(LOG_TAG, "check access: " + authData.getAccessToken() + ", refresh: " + authData.getRefreshToken());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top