cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Back again with another edition of 'How we use Dropbox', so find out how Emma uses to-do lists to get it all done 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: 

Oauth2 code short lived- how to implement correctly from B2B?

Oauth2 code short lived- how to implement correctly from B2B?

pwnell
Explorer | Level 4

I am rewriting the old authorization code of my Windows service that communicates with Dropbox on behalf of a single, fixed user account. I have hit a snag. Usually I use the client credentials grant type when implementing a B2B oauth2 based interface where there is no user interaction, no web server. It seems like I am stuck with authorization grant which causes issues.

I have manually requested a code going to the www.Dropbox.com/oauth2/authorize endpoint in a web browser, got the code and tried to use that as initial code when requesting an auth grant, followed by refresh tokens. The problem is that this code is short lived. So my service works for a couple of hours then fail when it fails to get a refresh token and tries to do another auth grant using that code.

So how am I to implement an oauth2 flow in Dropbox with no redirect URL/web server/client interaction?

1 Accepted Solution

Accepted Solutions

Re: Oauth2 code short lived- how to implement correctly from B2B?

Greg-DB
Dropboxer

The updated Dropbox app authorization flow does now use short-lived access tokens and refresh tokens. In either implementation, the initial authorization does require some manual user interaction.

 

With the new functionality, if you need long-term access (that is, longer than four hours) without further manual interaction after the initial authorization, you should request "offline" access. That way, during the initial authorization your app will receive both a short-lived access token as well as a refresh token.

 

Then, when the current short-lived access token has expired, the app should use the refresh token to request a new short-lived access token, by calling /oauth2/token with 'grant_type=refresh_token'. This step can be done entirely programmatically, without additional manual user interaction.

 

You can find more information in the following resources:

View solution in original post

5 Replies 5

Re: Oauth2 code short lived- how to implement correctly from B2B?

Greg-DB
Dropboxer

The updated Dropbox app authorization flow does now use short-lived access tokens and refresh tokens. In either implementation, the initial authorization does require some manual user interaction.

 

With the new functionality, if you need long-term access (that is, longer than four hours) without further manual interaction after the initial authorization, you should request "offline" access. That way, during the initial authorization your app will receive both a short-lived access token as well as a refresh token.

 

Then, when the current short-lived access token has expired, the app should use the refresh token to request a new short-lived access token, by calling /oauth2/token with 'grant_type=refresh_token'. This step can be done entirely programmatically, without additional manual user interaction.

 

You can find more information in the following resources:

View solution in original post

Re: Oauth2 code short lived- how to implement correctly from B2B?

pwnell
Explorer | Level 4
That is what I did. However usually the refresh token is also short lived. You are saying I should persist the refresh token and use it even if my service restarts, to fetch a new auth token?

Re: Oauth2 code short lived- how to implement correctly from B2B?

Greg-DB
Dropboxer

Yes, you should store and re-use the refresh token repeatedly. (Dropbox refresh tokens are not short-lived; they do not expire by themselves, though they can be revoked on demand by the user or app.)

Re: Oauth2 code short lived- how to implement correctly from B2B?

pwnell
Explorer | Level 4

Seems like I spoke too soon.  It worked for a week or two but now the refresh token expired and I had to do the manual get token, then get refresh token, add to my code, restart the service routing which is just not cool.

 

I got HTTP Status code Unauthorized back after the refresh token worked fine for a while.

Re: Oauth2 code short lived- how to implement correctly from B2B?

Greg-DB
Dropboxer

Dropbox OAuth 2 refresh tokens don't expire by themselves, but there are a number of ways they can become invalid. For example:

If something isn't working as expected though, please feel free to share the details (e.g., the steps and code to reproduce the issue, and the unexpected error/output) so we can look into it.

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
    Greg-DB Dropboxer
  • User avatar
    pwnell Explorer | Level 4
What do Dropbox user levels mean?
Need more support?