在 Android 上使用已保存的 Dropbox 身份验证详细信息 [英] Using saved Dropbox authentication details on Android

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

问题描述

在 Dropbox 网站上的入门文章中,他们提供了如何使用 Android 的 Core API 开始.

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

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

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.

在用户对应用程序进行身份验证以使用他或她的 Dropbox 帐户后,我使用 Android 的 SharedPreferences 保存密钥和机密.

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.

如何使用这些保存的值?我的意思不是我如何使用 SharedPreferences 检索它们,而是我如何使用这些来防止需要再次重新验证应用程序?在 Dropbox 网站上,他们没有提供使用这些的方法.他们说的是

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

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

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().

在您的应用程序关闭后,您将再次需要这些令牌,因此保存它们以备将来访问很重要(尽管此处未显示).如果不这样做,用户每次都必须重新进行身份验证使用您的应用程序.实现存储密钥的常用方法是通过Android 的 SharedPreferences API.

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.

推荐答案

我遇到了同样的问题,文档不是很好,所以我看到了几个相关的问题.解决这个问题的关键在于AndroidAuthSession类中的isLinked()方法.我分享我的代码,所以它可能会解决你的疑虑.

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

我会一步一步解释.

  • 首先,我使用 Dagger 作为依赖注入,这就是我在构造函数中获取 mDropBoxApi 的原因,但如果不是,请始终以与我在此方法中所做的相同的方式创建会话.

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

  • 既然您拥有 DropboxAPI 对象,您需要 startOAuth2Authentication' 或setOAuth2AccessToken` 以防您已经拥有它(从上次会话中保存).您可以在 onCreate(如果它是一个活动)或 onActivityCreated(如果它是一个 Fragment)中执行此操作.

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

  • 之后,在您的 onResume 方法中(我们将在这里解决我们的问题),您检查登录是否成功调用函数 session.authenticationSuccessful().只有在您进行了身份验证过程时,这才会返回 true.如果它不为空,则要么登录不成功,要么您的帐户已链接.就是这样,链接.你如何检查?正如我之前所说,这是解决这个问题的关键.您需要检查的是会话是否已经链接调用 session.isLinked() 和voilà.它会告诉您是否已成功与 dropbox api 链接,如果为 false,则说明过程中出现问题.

  • 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天全站免登陆