cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
If you’ve changed your email address, now's the perfect time to update it on your Dropbox account and we’re here to help! Learn 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: 

Get new AccessToken when access token expired (Android)

Get new AccessToken when access token expired (Android)

VIctorCZ
New member | Level 2
Go to solution

I apologize in advance for my English.

I have several android applications that use Dropbox to send and load data. 

Until the last update, Dropbox communication worked properly - probably an Access Token without expiring

 

Here is a sample of the code I use:

// Create Dropbox client
DbxRequestConfig config;
config = new DbxRequestConfig("dropbox/appName");
return new DbxClientV2(config, ACCESS_TOKEN);
// save Access Token
if
(ACCESS_TOKEN == null) {
String accessToken = Auth.getOAuth2Token(); //generate Access Token
if (accessToken != null) {
//add accessToken to SharedPreferences
SharedPreferences prefs = context.getSharedPreferences("com.appName.dropboxintegration", Context.MODE_PRIVATE);
prefs.edit().putString("access-token", accessToken).apply();
}
}
//load Acces token from SharedPreferences
SharedPreferences prefs = context.getSharedPreferences("com.appName.dropboxintegration", Context.MODE_PRIVATE);
String accessToken = prefs.getString("access-token", null);
if (accessToken == null) {
Log.d("AccessToken Status", "No token found");
return null;
} else {
//accessToken already exists
Log.d("AccessToken Status", "Token exists");
return accessToken;
}

 

// here it falls on expired
try
{
MoveFile();
} catch (InvalidAccessTokenException ex) {
endSend = null;
resetToken = true;
ex.printStackTrace();
} catch (DbxException e) {
endSend = null;
e.printStackTrace();
}

I tried to call OAuth again. - repeatedly reports the token has expired

I tried to terminate the token - it won't work

I tried to use Auth.startOAuth2PKCE () to log in - it reports a token malfunction

Auth.getOAuth2Token () does not return a new token

 

my questions:

How do I request a new AccessToken whenever I find out it's expired.

I can somehow request a token without expiring.

Do I need to convert the application to a short-term token?

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

For Android apps using the official Dropbox API v2 Java SDK, you should upgrade to the latest version of the SDK (currently v5.4.4) and use the updated authorization functionality, starting with the startOAuth2PKCE method. You can find an example app here. For instance, you can see where that starts in the code here and how the result is handled here. When you use that functionality as shown, the SDK will then handle the refresh process for you automatically.

--

Edit: updated version number and links.

View solution in original post

5 Replies 5

Greg-DB
Dropbox Staff
Go to solution

For Android apps using the official Dropbox API v2 Java SDK, you should upgrade to the latest version of the SDK (currently v5.4.4) and use the updated authorization functionality, starting with the startOAuth2PKCE method. You can find an example app here. For instance, you can see where that starts in the code here and how the result is handled here. When you use that functionality as shown, the SDK will then handle the refresh process for you automatically.

--

Edit: updated version number and links.

VIctorCZ
New member | Level 2
Go to solution

Thank you for your response and help. Using the new SDK, startOAuth2PKCE and DbxCredential, I got accessToken and refreshToken. It's working properly now.

sundar_papco
New member | Level 2
Go to solution

Hello!

 

Both the code links mentioned in the accepted answer are now dead. Kindly update the links please

Здравко
Legendary | Level 20
Go to solution

Hi @sundar_papco,

You can review all Dropbox Java SDK related examples here. 😉

Good luck.

Greg-DB
Dropbox Staff
Go to solution

@sundar_papco I've updated the links in my post. Thanks!

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    sundar_papco New member | Level 2
  • User avatar
    VIctorCZ New member | Level 2
What do Dropbox user levels mean?