Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
When I authenticate my application using OAuth2 method it works fine and I am able to save the token. However, when I reopen the application and try to use the saved token, the authentication always fails. I checked and the token that is created is the same as the one gets saved.
Any ideas what might be the issue?
Can you share a sample failing request and response? (Redact part of the access token to be safe though.)
If you're using the OAuth 2 "code" flow, one thing we sometimes see is that the app saves the authorization code instead of the access token. The access token is re-usable, but the authorization code isn't. (The authorization code is the string that you get first and then exchange for an access token.)
Here is my code. The issue is coming from the authenticationSuccessful failing after I create a new AndroidAuthSession with the previous access token.
// create the application key token
AppKeyPair appKeyToken = new AppKeyPair("APP Key", "APP Secret");
AndroidAuthSession session = new AndroidAuthSession(appKeyToken);
// try to get the session token from the shared preferences
String accessToken = getDropboxToken();
// check if the session token was retrieved
if (accessToken == null)
{
// if no token, then have the user sign in
dropboxSession = new DropboxAPI<>(session);
dropboxSession.getSession().startOAuth2Authentication(context);
}
else
{
// session token already available
dropboxSession = new DropboxAPI<>(new AndroidAuthSession(appKeyToken,accessToken));
// authenticate Dropbox login
authenticateDropboxLogin();
}
private String getDropboxToken()
{
// get the shared preferences area for the token
SharedPreferences sessionTokenRecord = context.getSharedPreferences("token", Context.MODE_PRIVATE);
// get the token key
String sessionToken = sessionTokenRecord.getString("accessToken", null);
return sessionToken;
}
private void saveDropboxToken(String accessToken)
{
// create a new shared preferences area for the token
SharedPreferences.Editor tokenRecordEditor = context.getSharedPreferences("token", Context.MODE_PRIVATE).edit();
tokenRecordEditor.putString("accessToken", accessToken);
tokenRecordEditor.commit();
}
private void authenticateDropboxLogin()
{
// check if the authentication was successful
if (dropboxSession.getSession().authenticationSuccessful())
{
// finish the authentication
try
{
dropboxSession.getSession().finishAuthentication();
saveDropboxToken(dropboxSession.getSession().getOAuth2AccessToken());
}
catch (IllegalStateException e)
{
showToast("Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
Log.i("Dropbox", "Error authenticating", e);
}
}
}
Can you also share the exception/stack trace? Thanks in advance!
That's the thing, there is not a stack trace/exception from building the new AndroidAuthSession, the authenticationSucessful() just returns false and finishAuthenication is never called. If I force the finishAuthentication to be called then it just says "Authentication Failed", but nothing else.
Also, this issue only occurs when the application is killed and reopened. If the application is paused and resumed then it works just fine.
Thanks James. You shouldn't call authenticateDropboxLogin or authenticationSuccessful or finishAuthentication when you already have an access token. The access token is all you need to access the account. That is, once you have your DropboxAPI object using the retrieved access token, you can start making API calls with it.
Now everything works perfectly, thank you very much! 🙂 I'm not sure why I thought you needed to have those extra steps with the access token.
Hi there!
If you need more help you can view your support options (expected response time for a ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!