Search code examples
pythondropbox

Dropbox python api oauth2 implementation


I'm trying to access my own account on dropbox with oauth2 short-lived tokens. this is the code I've written so far. It works for now but the access token seems to never expire. Am I donig this right? I have set the default token type to short lived on dropbox app console. Thanks

with open(path.join(project_folder, 'access_token'), 'r') as f:
    dbx = dropbox.Dropbox(f.read(), oauth2_refresh_token=getenv("DROPBOX_REFRESH_TOKEN"), app_key=app_key, app_secret=app_secret)

def refresh():
    old_token = dbx._oauth2_access_token
    dbx.check_and_refresh_access_token()
    new_token = dbx._oauth2_access_token
    if new_token != old_token:
        with open(path.join(project_folder, 'access_token'), 'w') as f:
            f.write(dbx._oauth2_access_token)
            print("token refreshed")

Solution

  • The sdk itself takes care of refreshing the token when a refresh token and app key and app secret are provided. So no need to supply an access token.