保存和使用身份验证数据中的Andr​​oid API身份验证、数据、Andr、oid

2023-09-06 22:56:21 作者:古木九年#

我创建一个盒子的Andr​​oid应用程序,允许用户上传自己的账户媒体文件。 我已经建立了我的客户ID和客户端密钥,它是验证我的应用程序了。 上传的部分也做了,但我面临的问题是保存在auth数据[这显然是必要的,这样用户不需要重新登录并再次]

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. Uploading part is also done,but the problem i am facing is to save the auth data [which is obviously needed so user is not needed to login again and again]

Load,保存在盒子的Andr​​oid API 使用验证数据 上面给出的解决方案是行不通的[可b,他们已删除了Utils.parseJSONStringIntoObject'方法]

Load, save and use of authentication data in Box Android API the solution given above is not working [may b they have removed 'Utils.parseJSONStringIntoObject' method]

我可以将访问令牌,令牌刷新,但什么储蓄,当我不能用它来重新验证用户的角度

i can store the access token and refresh token but whats the point of saving when i cant use them to re authenticate a user

    switch (requestCode) 
    {
        case AUTHENTICATE_REQUEST:
            if (resultCode == Activity.RESULT_CANCELED) 
            {
                String failMessage = data.getStringExtra(OAuthActivity.ERROR_MESSAGE);
                Toast.makeText(this, "Auth fail:" + failMessage, Toast.LENGTH_LONG).show();
            //    finish();
            }
            else 
            {
                BoxAndroidOAuthData oauth = data.getParcelableExtra(OAuthActivity.BOX_CLIENT_OAUTH);
                BoxAndroidClient client = new BoxAndroidClient(BoxSDKSampleApplication.CLIENT_ID, BoxSDKSampleApplication.CLIENT_SECRET, null, null);
                client.authenticate(oauth);
                String ACCESS_TOKEN=oauth.getAccessToken();
                String REFRESH_TOKEN=oauth.getRefreshToken();
                Editor editor = prefs.edit();
                editor.putString("ACCESS_TOKEN", ACCESS_TOKEN);
                editor.putString("REFRESH_TOKEN", REFRESH_TOKEN);
                editor.commit(); 


                BoxSDKSampleApplication app = (BoxSDKSampleApplication) getApplication();
                client.addOAuthRefreshListener(new OAuthRefreshListener() 
                {
                    @Override
                    public void onRefresh(IAuthData newAuthData) 
                    {
                        Log.d("OAuth", "oauth refreshed, new oauth access token is:" + newAuthData.getAccessToken());
                        //---------------------------------
                        BoxOAuthToken oauthObj=null;
                        try 
                        {
                             oauthObj=getClient().getAuthData(); 
                        }
                        catch (AuthFatalFailureException e) 
                {
                e.printStackTrace();
                }
                        //saving refreshed oauth object in client
                        BoxAndroidOAuthData newAuthDataObj=new BoxAndroidOAuthData(oauthObj);
                        getClient().authenticate(newAuthDataObj);

                    }

                });
                app.setClient(client);
            }

我已经提到 https://github.com/box /箱Android的SDK-V2 /树/主/ BoxSDKSample 例如

任何一个可以告诉我什么,我做错了,或任何其他使用的authData,访问令牌验证用户,刷新令牌?

can any one tell me what i am doing wrong or any alternative to authenticate user using authdata,access token,refresh token?

刷新令牌他们纷纷表示 我们的SDK自动刷新OAuth访问令牌时到期。你会想听听刷新事件,并刷新之后更新存储的令牌。

refreshing token as they have said 'Our sdk auto refreshes OAuth access token when it expires. You will want to listen to the refresh events and update your stored token after refreshing.'

mClient.addOAuthRefreshListener(new OAuthRefreshListener() 
                {
                    @Override
                    public void onRefresh(IAuthData newAuthData) 
                    {


                        Log.d("OAuth", "oauth refreshed, new oauth access token is:" + newAuthData.getAccessToken());
                        try 
                        {

                             oauthObj=mClient.getAuthData();
                             mClient.authenticate(newAuthData);

                             String authToken=null;
                                //Storing oauth object in json string format
                             try 
                             {
                                 authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(newAuthData);
                                 prefs.edit().putString("BOX_TOKEN", authToken).commit();
                                 //saving authToken in shared Preferences
                                 mClient.authenticate(newAuthData);
                                String ACCESS_TOKEN=newAuthData.getAccessToken();
                                String REFRESH_TOKEN=newAuthData.getRefreshToken();

                                Log.v("New Access token ", oauthObj.getAccessToken());
                                Log.v("New Refresh token ", oauthObj.getRefreshToken());

                                editor.putString("ACCESS_TOKEN", ACCESS_TOKEN);
                                editor.putString("REFRESH_TOKEN", REFRESH_TOKEN);
                                prefs.edit().putString("BOX_TOKEN", authToken).commit();
                                editor.commit();

                             }
                             catch (BoxJSONException e1) 
                             {
                                    e1.printStackTrace();
                             }
                            Log.v("Token Refreshed", " ");
                        }
                        catch (AuthFatalFailureException e) 
                        {
                            e.printStackTrace();
                        }
                    }
                });
                app.setClient(mClient);
            }

            onClientAuthenticated();

在主要活动,取存放令牌

In main activity,fetching stored token

                try 
            {
 stored_oauth_token=prefs.getString("BOX_TOKEN", null);
                authData = new BoxJSONParser(new AndroidBoxResourceHub()).parseIntoBoxObject(stored_oauth_token, BoxAndroidOAuthData.class);
            }
            catch (BoxJSONException e) 
            {
                e.printStackTrace();
            }
                mClient = new BoxAndroidClient(BoxSDKSampleApplication.CLIENT_ID, BoxSDKSampleApplication.CLIENT_SECRET, null, null);
                mClient.authenticate(authData);
                BoxSDKSampleApplication app = (BoxSDKSampleApplication) getApplication();
                app.setClient(mClient);

我想这个应用程序后,现有的上载文件,它的工作 但60-70多分钟后,我不能上传文件。 有什么错我的code?

i tried this app to upload a file after existing ,it did work but after 60-70 odd minutes i couldn't upload file. is there anything wrong in my code ?

推荐答案

这是我如何初始化我的盒子客户端:

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);
}