cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: 

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

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

Leah C.1
New member | Level 1

Glad to hear you got it working!

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

Need more support?