使用Android上保存的Dropbox的认证细节 [英] Using saved Dropbox authentication details on Android

查看:295
本文介绍了使用Android上保存的Dropbox的认证细节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

入门上的Dropbox网站的文章,他们有一个教程如何使用核心API与Android上手。

当应用程序首次启动时,系统会要求用户进行身份验证使用用户的Dropbox帐户的软件。经过身份验证成功完成后,您会收到一对身份验证字符串,密钥和秘密。

在用户认证使用他或她的Dropbox帐户我保存密钥和应用程序的使用Android的共享preferences secred。

我如何使用这些保存的值?我不是说我怎么找回它们使用共享preferences但我该如何使用这些prevent再次重新验证应用的需要?在Dropbox的网站,他们不提供一种方式来使用这些。所有他们说的是

  

在finishAuthentication()方法将绑定用户的访问令牌   到会话。现在,您就可以通过检索它们   mDBApi.getSession()。getAccessTokenPair()。

     

您会再次需要这些标记您的应用程序关闭后,所以它的   重要的是要保存以供日​​后使用(虽然它不是如图所示)。   如果不这样做,用户将不得不每次都重新验证他们   使用你的应用程序。实现存储密钥一种常见的方式是通过   Android的共享preferences API。

解决方案

我有同样的问题,该文件是不是很大,所以我看到了有关这几个相关的问题。 解决这个问题的关键是在方法 isLinked() AndroidAuthSession 类。我分享我的code所以它可能会解决你的疑惑。

 公共类DropBoxInteractorImpl实现DropBoxInteractor {
    私人DropboxAPI< AndroidAuthSession> mDropBoxApi;

    公共DropBoxInteractorImpl(DropboxAPI< AndroidAuthSession> mDropBoxApi){
        this.mDropBoxApi = mDropBoxApi;
    }

    @覆盖
    公共无效的登录(最终上下文的背景下,最终CloudServiceCallback cloudServiceCallback){
        字符串accessToken = prefUtils.getFrom preFS(背景下,按键pref.DROPBOX_ACCESS_TOKEN,NULL);
        如果(accessToken == NULL){
            。mDropBoxApi.getSession()startOAuth2Authentication(上下文);
        } 其他 {
            。mDropBoxApi.getSession()setOAuth2AccessToken(accessToken);
        }
    }

    @覆盖
    公共无效confirmLogin(上下文的背景下,最终CloudServiceCallback cloudServiceCallback){
        AndroidAuthSession会话= mDropBoxApi.getSession();
        如果(session.authenticationSuccessful()){
            //按要求完成身份验证,设置在会话访问令牌
            session.finishAuthentication();
            字符串accessToken = mDropBoxApi.getSession()getOAuth2AccessToken()。
            prefUtils.saveTo preFS(背景下,按键pref.DROPBOX_ACCESS_TOKEN,accessToken);
        }

        如果(session.isLinked()){
            cloudServiceCallback.loginSuccessful();
        } 其他 {
            cloudServiceCallback.loginError();
            Timber.e(有一个问题,登录进来!);
        }
    }
}
 

我会解释它一步一步来。

    所有的
  • 首先,我用匕首作为依赖注入,这就是为什么我让我在构造函数中mDropBoxApi,但如果你不是,只是创建会话总是以同样的方式,因为我在做这个方法。

      @Provides
    @Singleton
    公共DropboxAPI< AndroidAuthSession> providesDropBoxService(){
        AppKeyPair appKeyPair =新AppKeyPair(Keys.DROPBOX_APP,Keys.DROPBOX_APP_SECRET);
        AndroidAuthSession会议=新AndroidAuthSession(appKeyPair);
        返回新DropboxAPI< AndroidAuthSession>(会议);
    }
     

  • 现在,你有你DropboxAPI对象,您需要为 startOAuth2Authentication或 setOAuth2AccessToken`如果你有它已经(保存的表单上一届会议)。可以这样做,在的onCreate(如果它是一个活动)或在onActivityCreated如果它是一个片段。

      @覆盖
    公共无效的登录(最终上下文的背景下){
        字符串accessToken = prefUtils.getFrom preFS(背景下,按键pref.DROPBOX_ACCESS_TOKEN,NULL);
        如果(accessToken == NULL){
            。mDropBoxApi.getSession()startOAuth2Authentication(上下文);
        } 其他 {
            。mDropBoxApi.getSession()setOAuth2AccessToken(accessToken);
        }
    }
     

  • 在此之后,在你的onResume方法(在这里就是我们要解决我们的问题),你是否登录成功调用函数 session.authenticationSuccessful()。这将返回只有在情况下,你做了认证过程如此。如果不为空,或者登录不成功或您的帐户已链接。就这样,挂。你怎么检查?那么正如我以前说过,它的关键是解决了这个问题。如果会话已链接调用 session.isLinked(),瞧你需要检查什么。它会告诉你,如果你成功地与Dropbox的API,或在情况下,它是假的链接,有在这个过程中的一个问题。

      @覆盖
    公共无效confirmLogin(上下文的背景下,最终CloudServiceCallback回调){
        AndroidAuthSession会话= mDropBoxApi.getSession();
        如果(session.authenticationSuccessful()){
            //按要求完成身份验证,设置在会话访问令牌
            session.finishAuthentication();
            字符串accessToken = mDropBoxApi.getSession()getOAuth2AccessToken()。
            prefUtils.saveTo preFS(背景下,按键pref.DROPBOX_ACCESS_TOKEN,accessToken);
        }
    
        如果(session.isLinked()){
            callback.loginSuccessful();
        } 其他 {
            callback.loginError();
            Timber.e(有一个问题,登录进来!);
        }
    }
     

我希望这个解决您怀疑这件事了,如果你有任何问题,请不要犹豫,问。

In the Getting started article on Dropbox website they have a tutorial how to use Core API with Android to get started.

When the application launches for the first time, the user is asked to authenticate the software to use the users Dropbox account. After the authentication is successfully finished, you'll receive a pair of authentication strings, the key and a secret.

After the user has authenticated the application to use his or her Dropbox account I save the key and the secred using Android's SharedPreferences.

How do I use these saved values? I don't mean how I retrieve them using SharedPreferences but how I use these to prevent the need of reauthenticating the application again? On the Dropbox site they don't provide a way to use these. All they say is

The finishAuthentication() method will bind the user's access tokens to the session. You'll now be able to retrieve them via mDBApi.getSession().getAccessTokenPair().

You'll need these tokens again after your app closes, so it's important to save them for future access (though it's not shown here). If you don't, the user will have to re-authenticate every time they use your app. A common way to implement storing keys is through Android's SharedPreferences API.

解决方案

I had the same problem, The documentation is not great so I saw several related questions about it. The key to solve this problem is in the method isLinked() in the AndroidAuthSession class. I share my code so It might solve your doubts.

public class DropBoxInteractorImpl implements DropBoxInteractor {
    private DropboxAPI<AndroidAuthSession> mDropBoxApi;

    public DropBoxInteractorImpl(DropboxAPI<AndroidAuthSession> mDropBoxApi) {
        this.mDropBoxApi = mDropBoxApi;
    }

    @Override
    public void login(final Context context, final CloudServiceCallback cloudServiceCallback) {
        String accessToken = PrefUtils.getFromPrefs(context, KeysPref.DROPBOX_ACCESS_TOKEN, null);
        if (accessToken == null) {
            mDropBoxApi.getSession().startOAuth2Authentication(context);
        } else {
            mDropBoxApi.getSession().setOAuth2AccessToken(accessToken);
        }
    }

    @Override
    public void confirmLogin(Context context, final CloudServiceCallback cloudServiceCallback) {
        AndroidAuthSession session = mDropBoxApi.getSession();
        if (session.authenticationSuccessful()) {
            // Required to complete auth, sets the access token on the session
            session.finishAuthentication();
            String accessToken = mDropBoxApi.getSession().getOAuth2AccessToken();
            PrefUtils.saveToPrefs(context, KeysPref.DROPBOX_ACCESS_TOKEN, accessToken);
        }

        if (session.isLinked()) {
            cloudServiceCallback.loginSuccessful();
        } else {
            cloudServiceCallback.loginError();
            Timber.e("There was a problem login in!!");
        }
    }
}

I will explain it step by step.

  • First of all, I am using Dagger as dependency injection, that's why I get my mDropBoxApi in the constructor, but If you are not, just create the session always the same way as I am doing in this method.

    @Provides
    @Singleton
    public DropboxAPI<AndroidAuthSession> providesDropBoxService() {
        AppKeyPair appKeyPair = new AppKeyPair(Keys.DROPBOX_APP, Keys.DROPBOX_APP_SECRET);
        AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
        return new DropboxAPI<AndroidAuthSession>(session);
    }
    

  • Now that you have your DropboxAPI object, you need to either startOAuth2Authentication' orsetOAuth2AccessToken` in case you have it already (saved form the last session). You can do that in onCreate (if it is an activity) or in onActivityCreated if it is a Fragment.

    @Override
    public void login(final Context context) {
        String accessToken = PrefUtils.getFromPrefs(context, KeysPref.DROPBOX_ACCESS_TOKEN, null);
        if (accessToken == null) {
            mDropBoxApi.getSession().startOAuth2Authentication(context);
        } else {
            mDropBoxApi.getSession().setOAuth2AccessToken(accessToken);
        }
    }
    

  • After that, in your onResume method (and here is where we are going to solve our problem) you check if the login was successful calling the function session.authenticationSuccessful(). This will return true ONLY in case you did the authentication process. If it is not null, either the login was not successful or your account is already LINKED. That's it, LINKED. How do you check that? Well as I said before, It is the key to solved this problem. What you need to check is if the session is already Linked calling session.isLinked() and voilà. It will tell you if you are successfully linked with the dropbox api or , in case it is false, there was a problem in the process.

    @Override
    public void confirmLogin(Context context, final CloudServiceCallback callback) {
        AndroidAuthSession session = mDropBoxApi.getSession();
        if (session.authenticationSuccessful()) {
            // Required to complete auth, sets the access token on the session
            session.finishAuthentication();
            String accessToken = mDropBoxApi.getSession().getOAuth2AccessToken();
            PrefUtils.saveToPrefs(context, KeysPref.DROPBOX_ACCESS_TOKEN, accessToken);
        }
    
        if (session.isLinked()) {
            callback.loginSuccessful();
        } else {
            callback.loginError();
            Timber.e("There was a problem login in!!");
        }
    }
    

I hope this solve you doubts about it, and If you have any question, please, don't hesitate to ask.

这篇关于使用Android上保存的Dropbox的认证细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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