We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.

Forum Discussion

tuomo s.'s avatar
tuomo s.
New member | Level 1
10 years ago

How to use access token in Dropbox authentication in Android app

Hello. I'm trying to make an Android application that uses Dropbox. I managed to upload some files to Dropbox so that is working. But I would like to store the access token so that the user doesn't need to authenticate every time. Your tutorial says:

"
The finishAuthentication() method will bind the user's access token to the session. You'll now be able to retrieve it via mDBApi.getSession().getOAuth2AccessToken().

You'll need this token again after your app closes, so it's important to save it for future access (though it's not shown here). If you don't, the user will have to re-authenticate every time they use your app. A common way to implement storing keys is through Android's SharedPreferences API.
"

Now I have that accesstoken but how I can authenticate using it? I tried mDBApi.getSession().setOAuth2AccessToken(accessToken) but then putFile gives a DropboxUnlinkedException.

  • tuomo s.'s avatar
    tuomo s.
    New member | Level 1

    It seems to be working now in the way that I wanted. All I did was I moved

    session.setOAuth2AccessToken(accessToken);      
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    
  • tuomo s.'s avatar
    tuomo s.
    New member | Level 1

    I accidentally sent the answer before I had written it fully.
    So I did setOAuth2AccessToken() to session instead of mDBApi.getSession().

    In other words I changed the order in which I used setOAuth2AccessToken and mDBApi= new DropboxAPI...
    I don't know what was the error and now I'm not even sure if there was error

  • Demo B.'s avatar
    Demo B.
    New member | Level 1

    Actually I am also working on a similar kind of app and facing the same error as you did tuomo s. So kindly help me.. solving it.As the solution you provided is not working in my case..

  • tuomo s.'s avatar
    tuomo s.
    New member | Level 1

    So after you have authenticated (written your username and password) you run for example something like this in onResume that saves your accessToken to your preferences.

     

    protected void onResume() {
    super.onResume();

    String accessToken="DEFAULT";
    if (mDBApi.getSession().authenticationSuccessful()
    && !mDBApi.getSession().isLinked()) {

    mDBApi.getSession().finishAuthentication();

    accessToken = mDBApi.getSession()
    .getOAuth2AccessToken();

    SharedPreferences settings = getSharedPreferences("my_settings", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("dropboxtoken", accessToken);
    editor.commit();

    }

    }

     

    Next time you start your app you run something like this:

     

    SharedPreferences settings = getSharedPreferences("my_settings", 0);
    accessToken = settings.getString("dropboxtoken", "DEFAULT");
    if(!accessToken.equals("DEFAULT")){
    MyService.mDBApi.getSession().setOAuth2AccessToken(accessToken);

    }

    else {

    // accesstoken was not stored so start authentication 

    }

     

     

  • travis h.4's avatar
    travis h.4
    New member | Level 1

    I Know it's a little old but do you know how to create a similar method for the java core API? Ive been trying to use different methods to store the accessToken with no success...Any help or recommendation on what you guys did for you App would be appreciated, thank you!

About Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,875 PostsLatest Activity: 2 years ago
323 Following

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