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.
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"
<YOUR_REDIRECT_URI>/?code=<AUTHORIZATION_CODE>&state=12345
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%
{
"access_token": "<ACCESS_TOKEN>",
"expires_in": 14400,
"token_type": "bearer",
"refresh_token": "<REFRESH_TOKEN>",
"scope": "<SCOPES>",
"uid": "<USER_ID>",
"account_id": "<ACCOUNT_ID>"
}
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!