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.