cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
We love to hear how every person gets the most from Dropbox, and it seems everyone has a slightly different use for it - tell us yours 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: 

Dropbox SDK JS + Refresh token problem

Dropbox SDK JS + Refresh token problem

Alex57600
Explorer | Level 3

Hello

 

Please, do you have any idea how i can manage this ? Each 4 hours i get "expired_access_token" and user must re-log.

 

I just upgrade my old Dropbox SDK JS to the last version 10.8.0 + I did the changes to be compatible.

I have one Cordova app (iOS/Android/Win10).

The goal of my app is to : 1. User connect on dropbox (long life), user set "working hours" on my app, then each day/week user save/load data to/from Dropbox.

 

 

Here's my code:

 

Connexion code:

 

 

 

// 1. I init Dropbox connexion
dbx = new Dropbox.Dropbox({
  clientId: dropboxClientId
});



// 2. I get auth url with redirect (custom url scheme)
var dropboxRedirectUri = 'MYAPP://dropbox_callback';

dbx.auth.getAuthenticationUrl(
  dropboxRedirectUri,
  '',
  'code',
  'offline',
  [
    'files.content.write',
    'files.content.read',
  ],
  'user',
  true
)
.then((authUrl) => {
  dropboxCodeVerifier = dbx.auth.codeVerifier;
  cordova.InAppBrowser.open(authUrl, '_system');
});

// 3. User is redirect to his web browser to logoff, then user is redirected to my app
function handleOpenURL(url) {
  // I read the "code" send by Dropbox
  if (getUrlVars(url).code) {
   dbx.auth.setCodeVerifier(dropboxCodeVerifier);
   dbx.auth.getAccessTokenFromCode(dropboxRedirectUri, getUrlVars(url).code)
   .then((response) => {
      // I get the "access_token" (starts with sl as short-life)     
      localStorage['dropbox_access_token'] = response.result.access_token;
   })
   .then((response) => {
     ... User is now connected
   })
   .catch((error) => {
     alert(error);
    });
}

// At this point, i also get "refresh_token", but I'm sorry i don't know how to use it.
// So, after that, for each user action (save/load) i connect to dropbox + i launch my // actions with this code:

dbx = new Dropbox.Dropbox({
  accessToken: localStorage['dropbox_access_token']
});
dbx.filesUpload({...	



Best regards

 

 

 

 

2 Replies 2

Re: Dropbox SDK JS + Refresh token problem

Greg-DB
Dropboxer

To maintain long-term access, you'll need to request "offline" access, as you're doing, and then supply the app key (the app secret isn't needed since you're using PKCE) and refresh token. As long as you do so, the SDK will handle the refresh process for you automatically. You can find an example of setting the refresh token in this example.

Re: Dropbox SDK JS + Refresh token problem

Alex57600
Explorer | Level 3
Hello

Thank you very much.

I will try asap.
Who's talking

Top contributors to this post

  • User avatar
    Alex57600 Explorer | Level 3
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?