Your workflow is unique 👨‍💻 -  tell us how you use Dropbox here.

Forum Discussion

james b.91's avatar
james b.91
New member | Level 1
9 years ago

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

Replies have been turned off for this discussion
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    9 years ago

    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's avatar
    james b.91
    New member | Level 1
    9 years ago

    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's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    9 years ago

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

  • james b.91's avatar
    james b.91
    New member | Level 1
    9 years ago

    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's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    9 years ago

     

    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's avatar
    james b.91
    New member | Level 1
    9 years ago

    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.

About Dropbox API Support and Feedback

Node avatar for 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!