save and use auth data in box android API

前端 未结 2 1764
无人及你
无人及你 2021-01-16 09:16

I am creating an box android app that allows user to upload media files on their account. I have set up my client id and client secret,it is authenticating my app too. Uploa

相关标签:
2条回答
  • 2021-01-16 09:33

    So for the auth refresh there are a couple of things to be considered:

    1. box client automatically refreshes OAuth tokens, you'll want to attach a OAuthRefreshListener to listen to the refresh, if you want to persist, persist the oauth data passed into the refresh listener. The listener only update your persisted oauth data, you don't need to re-authenticate in the refresh listener, sdk does the re-authenticate automatically.
    2. When you first initiate box client, you need to authenticate either by persisted auth, or the OAuth UI. The logic should be:

      check client.isAuthenticated();

      2.1 If authenticated, do nothing.

      2.2 if not authenticated, try to check whether there's persisted auth data. If so, authenticate by client.authenticate(oauthdata);

      2.3 if 2.2 failed, start OAuth UI flow.

      2.4 at last, in case of OAuthFatalFailureException, start OAuth UI flow.

    0 讨论(0)
  • 2021-01-16 09:43

    This is how I initialize my Box client:

    mClient = new BoxClient(BOX_CLIENT_ID, BOX_CLIENT_SECRET, null, null);
    mClient.addOAuthRefreshListener(new OAuthRefreshListener() {
      @Override
      public void onRefresh(IAuthData newAuthData) {
        try {
          String authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(newAuthData);
          SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
          prefs.edit().putString("box_token", authToken).commit();
        } catch (BoxJSONException e) { }
      }
    });
    
    mAuthToken = prefs.getString("box_token", null);
    if (mAuthToken != null) {
      BoxAndroidOAuthData authData = new BoxJSONParser(
        new AndroidBoxResourceHub()
      ).parseIntoBoxObject(mAuthToken, BoxAndroidOAuthData.class);
      mClient.authenticate(authData);
    }
    
    if (!mClient.isAuthenticated()) {
      Intent intent = OAuthActivity.createOAuthActivityIntent(context, BOX_CLIENT_ID, BOX_CLIENT_SECRET, false, "https://yoururl.com/");
      ((Activity) context).startActivityForResult(intent, BOX_AUTH_REQUEST_CODE);
    }
    
    0 讨论(0)
提交回复
热议问题