save and use auth data in box android API

前端 未结 2 1765
无人及你
无人及你 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: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);
    }
    

提交回复
热议问题