Using Saved Dropbox Authentication Data on Android

In the Getting Started article on the Dropbox website, they have a tutorial on using the Core API with Android to get started.

When the application is launched for the first time, the user is prompted to authenticate the software to use the Dropbox user account. Upon successful authentication, you will receive a couple of authentication lines, a key and a secret.

After the user authenticated the application to use their Dropbox account, I keep the key and secret using Android SharedPreferences.

How to use these saved values? I do not mean how I retrieve them using SharedPreferences, but how do I use them to prevent the application from being re-authenticated again? Dropbox does not provide a way to use them. All they say

The finishAuthentication () method will bind the user's access tokens to the session. Now you can get them through mDBApi.getSession (). getAccessTokenPair ().

You will need these tokens again after closing your application, so it is important to keep them for future access (although it is not shown here). If you do not, the user will have to re-authenticate each time they use their application. A common way to implement Android SharedPreferences API storage keys.

+4
source share
3 answers

I had the same problem. The documentation is small, so I saw several related questions. The key to solving this problem is the isLinked() method in the AndroidAuthSession class. I share my code so that it can 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 this step by step.

  • First of all, I use Dagger as a dependency injection, so I get my mDropBoxApi in the constructor, but if you do not, just create a session just like I do 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' or setOAuth2AccessToken`, if you already have it (saved in the last session). You can do this in onCreate (if it's an activity) or inActivityCreated if it's 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 we are going to solve our problem), you check whether the login to the session.authenticationSuccessful() function was successful. This will return true ONLY if you have completed the authentication process. If it is not null, or the login was not successful, or your account is already tied. This is it, RELATED. How do you check this? As I said, this is the key to solving this problem. What you need to check is the session already associated with calling session.isLinked() and voilà. He will tell you if you are successfully associated with apbox Dropbox or, if it is a lie, there is 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 resolves your doubts about this, and if you have any questions, please feel free to ask.

+6
source

The JavaDoc kernel with Dropbox and Android extends what you need to do a little more, showing an alternative AndroidAuthSession constructor:

When the user returns to your application and you have tokens, just create a new session with them:

 AndroidAuthSession session = new AndroidAuthSession( myAppKeys, myAccessType, new AccessTokenPair(storedAccessKey, storedAccessSecret)); 

I assume that you just need to instantiate the DropboxAPI object, and you are good to go without startAuthentication () ... endAuthentication (), etc.

 DropboxAPI<AndroidAuthSession> mDBApi = new DropboxAPI<AndroidAuthSession>(session); 

I have not tried this, but it is essentially the same as Greg said.

+3
source

The samples included in the SDK show various ways to create a session with an existing access token. For example, using the setAccessTokenPair method:

  // Load state. State state = State.load(STATE_FILE); // Connect to Dropbox. WebAuthSession session = new WebAuthSession(state.appKey, WebAuthSession.AccessType.APP_FOLDER); session.setAccessTokenPair(state.accessToken); DropboxAPI<?> client = new DropboxAPI<WebAuthSession>(session); 

Or using the constructor:

  WebAuthSession sourceSession = new WebAuthSession(state.appKey, Session.AccessType.DROPBOX, sourceAccess); DropboxAPI<?> sourceClient = new DropboxAPI<WebAuthSession>(sourceSession); 

(These simple examples just load the access token from the status file.)

+2
source

Source: https://habr.com/ru/post/1489855/


All Articles