在 box android API 中保存和使用身份验证数据 [英] save and use auth data in box android API

查看:23
本文介绍了在 box android API 中保存和使用身份验证数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个盒子 android 应用程序,允许用户在他们的帐户上上传媒体文件.我已经设置了我的客户端 ID 和客户端密码,它也在验证我的应用程序.上传部分也完成了,但我面临的问题是保存认证数据[这显然是需要的,所以用户不需要一次又一次地登录]

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]

加载、保存以及在 Box Android API 中使用身份验证数据上面给出的解决方案不起作用 [可能他们已经删除了 '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/box-android-sdk-v2/tree/master/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 分钟后,我无法上传文件.
我的代码有什么问题吗?

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 ?

推荐答案

这是我初始化我的 Box 客户端的方式:

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

这篇关于在 box android API 中保存和使用身份验证数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆