Your workflow is unique 👨💻 - tell us how you use Dropbox here.
Forum Discussion
james b.91
9 years agoNew member | Level 1
Android SDK saved access token fails
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 ...
james b.91
9 years agoNew member | Level 1
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);
}
}
}
About Dropbox API Support and Feedback
Get help with the Dropbox API from fellow developers and experts.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
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!