cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Android SDK saved access token fails

Android SDK saved access token fails

james b.91
New member | Level 1

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? 

6 Replies 6

Greg-DB
Dropbox Staff

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

james b.91
New 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);
}
}
}

Greg-DB
Dropbox Staff

Can you also share the exception/stack trace? Thanks in advance! 

james b.91
New member | Level 1

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.

 

Greg-DB
Dropbox Staff

 

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.

james b.91
New member | Level 1

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.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    james b.91 New member | Level 1
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?