cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you an educator? Well we want to hear from you, so check out some tools we think might help you out 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: 

How to use access token in Dropbox authentication in Android app

How to use access token in Dropbox authentication in Android app

tuomo s.
New member | Level 1

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.

6 Replies 6

Re: How to use access token in Dropbox authentication in Android app

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

Re: How to use access token in Dropbox authentication in Android app

Leah C.1
New member | Level 1

Glad to hear you got it working!

Re: How to use access token in Dropbox authentication in Android app

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

Re: How to use access token in Dropbox authentication in Android app

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

Re: How to use access token in Dropbox authentication in Android app

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 

}

 

 

Re: How to use access token in Dropbox authentication in Android app

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!

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    travis h.4 New member | Level 1
  • User avatar
    tuomo s. New member | Level 1
  • User avatar
    Demo B. New member | Level 1
  • User avatar
    Leah C.1 New member | Level 1
What do Dropbox user levels mean?
Need more support?