'How to correctly implement refresh token with Dropbox Python SDK?

I'm trying to use the Dropbox Python SDK but was continually running into an issue of the short lived access token expiring. I think I finally found a solution but I'm fairly certain it's not how I should implementing it. The process I used is as follows:

  1. Copied example code from the official Dropbox Python SDK github
  2. Added print(f"Refresh token: {oauth_result.refresh_token}") after line 20 to actually see the REFRESH_TOKEN
  3. Changed my script from using dbx = dropbox.Dropbox(dbx_token) to dbx = dropbox.Dropbox(oauth2_refresh_token=REFRESH_TOKEN, app_key=APP_KEY). Where REFESH_TOKEN is what was printed from the example script.

If this is entirely wrong please let me know. Yet it seems to work as of now.

Side note: The purpose of the script I'm working on is to check specific Dropbox folders for new files once daily and then send share links to specific slack channels my team uses. Not sure if that is relevant, but decided to include just in case.



Solution 1:[1]

Modifying your OAuth URL to include token_access_type='offline' and initializing dropbox.Dropbox with the refresh token & app key should be correct.

Solution 2:[2]

Dropbox refresh tokens are reusable and do not expire until they are explicitly revoked. Once you have obtained the refresh token, you only need to use it as a parameter to create the dropbox object.

import dropbox

dbx = dropbox.Dropbox(
            app_key = <APP_KEY>,
            app_secret = <APP_SECRET>,
            oauth2_refresh_token = <REFRESH_TOKEN>
        )

If you need help to get the refresh token refer this answer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kyle
Solution 2 Sparrow