How to get a long lived access token (refresh token) for Dropbox

Peter posted this on January 22, 2025

Over the weekend, I was building an integration to automate my new podcast, Green Tech Pulse and while everything else worked, the Dropbox integration kept failing after a while.

Looking at the error log, I realised the Access Token which I got from Dropbox was expiring.

A little research led me to realise Dropbox stopped creating lon-lived access tokens on September 30, 2021 meaning my App would only get Access Tokens with a lifetime of 14,400 seconds (4 hours).

The alternative was getting a refresh token and there was no straightforward guide on creating one.

That’s why I decided to make this guide on how to get a refresh token for Dropbox.

I hope it helps someone.

Steps.

    1. Create a dropbox app by visiting https://dropbox.com/developers/apps

      Choose the settings for your app:
      Choose an API: Scoped access.
      Type of access: Full Dropbox or App folder
      Name: Give your app a unique name.
      Click Create App
      Enable Additional Users (Very Important)
      Set a Redirect URL (you can use http://localhost)
      Save the new app’s App Key and App Secret
    2.  Go into your CLI (I’m using the windows CMD) and enter the following then press enter:

      set APP_KEY=<YOUR_APP_KEY>
      set REDIRECT_URI=<YOUR_REDIRECT_URI>
      set STATE=12345
      echo "https://www.dropbox.com/oauth2/authorize?client_id=%APP_KEY%&response_type=code&token_access_type=offline&redirect_uri=%REDIRECT_URI%&state=%STATE%"
      This would generate a link in the following format"https://www.dropbox.com/oauth2/authorize?client_id=<NEW_CLIENT_ID>&response_type=code&token_access_type=offline&redirect_uri=<YOUR_REDIRECT_URI>&state=12345"
    3. Take the link generated and put it in your browser (this won’t work if you didn’t enable additional users),
      If it tells you to login, please do, after login, it would take you to a new page in the format
      <YOUR_REDIRECT_URI>/?code=<AUTHORIZATION_CODE>&state=12345
      Copy out the Authorization code from the link in your browser and save it.
    4. The next step would be to use the Authorization code to get an Access Token and Refresh Token and for this you need to go back to your Windows CMD or cli and enter the following
      Replace <AUTHORIZATION_CODE> and <YOUR_APP_SECRET>with what you saved earlier
      set AUTHORIZATION_CODE=<AUTHORIZATION_CODE>
      set APP_SECRET=<YOUR_APP_SECRET>
      curl -X POST https://api.dropbox.com/oauth2/token ^
      -d code=%AUTHORIZATION_CODE% ^
      -d grant_type=authorization_code ^
      -d client_id=%APP_KEY% ^
      -d client_secret=%APP_SECRET% ^
      -d redirect_uri=%REDIRECT_URI%
    5. This would generate code in the following format containing both your Access Token (expiring in 4 hrs) and Refresh Token!
      {
      "access_token": "<ACCESS_TOKEN>",
      "expires_in": 14400,
      "token_type": "bearer",
      "refresh_token": "<REFRESH_TOKEN>",
      "scope": "<SCOPES>",
      "uid": "<USER_ID>",
      "account_id": "<ACCOUNT_ID>"
      }

      Save the refresh token as you can always use it to get new access tokens
    6. To get a new access token with the refresh token, you can use the following in your CMD
      set REFRESH_TOKEN=<REFRESH_TOKEN>
      curl -X POST https://api.dropbox.com/oauth2/token ^
      -d refresh_token=%REFRESH_TOKEN% ^
      -d grant_type=refresh_token ^
      -d client_id=%APP_KEY% ^
      -d client_secret=%APP_SECRET%
      You can now use this refresh token to always generate a new access token in your code whenever a 401 error is encountered!

If this helped you, don’t forget to say thanks below and subscribe to my Tech Your Business Podcast!

Post A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.