Forum Discussion

HampdenTech's avatar
HampdenTech
New member | Level 1
2 months ago

Dropbox API - check_and_refresh_access_token

I have a valid app_key, app_secret, and refresh_token and I'm trying to call the check_and_refresh_access_token function in the Dropbox API for Python. Here is the code...

def refresh_dropbox_token(app_key, app_secret, refresh_token):
   dbx = dropbox.Dropbox(
   oauth2_refresh_token=refresh_token,
   app_key=app_key,
   app_secret=app_secret,
   )
   token = dbx.check_and_refresh_access_token()
   if token:
      return token.access_token
   else:
      raise Exception("Failed to refresh access token.")

In the debugger when I examine the token variable it has the value of nothing. Just to make sure I was passing a valid refresh_token, I called this with the refresh_token received in a full oauth2 authorization process. 

Any ideas on what may be wrong here? Once I do the full authorization I am able to upload files through the API, but when it expires, I want to use the refresh_token rather than taking the user back to Dropbox to authorize again.

Any help on this would be greatly appreciated!
Bob

 

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    The check_and_refresh_access_token method doesn't actually return a token value; it would either internally update the access token in the client, or otherwise raise an exception, so you don't need to check the return value.

    Also, you don't actually need to call check_and_refresh_access_token yourself at all. The Dropbox client will automatically call check_and_refresh_access_token itself as needed. You just need to set up the Dropbox object with your refresh token, app key, and app secret as you have and use that. It will automatically handle the refresh process for you internally.

    • PHAnetwork's avatar
      PHAnetwork
      Helpful | Level 5

      I'm just getting back to this issue now.

      Passing the refresh token when instantiating the Dropbox object seems to work fine.

      Thanks again for the help and Happy New Year!

  • HampdenTech's avatar
    HampdenTech
    New member | Level 1

    Hi Greg,

    Okay, this is great to know. I'll give this a try.


    Thanks for the update!

    Bob