cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
If you’ve changed your email address, now's the perfect time to update it on your Dropbox account and we’re here to help! Learn more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can I automate retrieval of a refresh token using the Python API?

How can I automate retrieval of a refresh token using the Python API?

jgriffo1
New member | Level 2

I have a Python app that is intended to run as a scheduled job to save files to Dropbox at regular intervals.  When I have a refresh token, this works fine:

dbx = dropbox.Dropbox(app_key='xxx', app_secret='yyy', oauth2_refresh_token='zzz')

 

But of course, the token only lasts for four hours. Every example I've found (here and here) all lead to answers that require a manual login to retrieve a refresh token. That will not help me to automate this job.

 

What can I do to get some sort of "long running" token or to be able to automatically generate refresh tokens without having to log in manually?

6 Replies 6

Greg-DB
Dropbox Staff

[Cross-linking for reference: https://stackoverflow.com/questions/71955472/how-do-i-automate-retrieval-of-a-refresh-token-using-th... ]

 

Dropbox API refresh tokens do not expire after four hours. They can be revoked on demand, but they do not expire automatically, and can be stored and re-used repeatedly. (The short-lived access tokens themselves do expire, but the refresh token can be used to get new short-lived access tokens whenever needed. The official Dropbox SDKs will do that process for you automatically.)

 

So, while you do need to manually process the app authorization flow once to get a refresh token, once you have it, you can store and re-use it for long-term access without further manual interaction.

jgriffo1
New member | Level 2

Thank you for your response.  I think my confusion came from the JSON that had an expiry field on it:

{
"access_token": "www",
"token_type": "bearer",
"expires_in": 14400,
"refresh_token": "xxx",
"scope": "account_info.read files.content.write files.metadata.read files.metadata.write",
"uid": "yyy",
"account_id": "zzz"
}

Greg-DB
Dropbox Staff

I see, thanks for the context. That expiration only applies to the access token. You can find more information on each field in the documentation here.

igor-plotnikov
New member | Level 2

The page you referenced here won't open. Could you please share the snippet of how to get an access token having a refresh token and using the official Python SDK?

Greg-DB
Dropbox Staff

@igor-plotnikov Can you clarify what you mean when you say the page won't open? Are you getting some error? That link is working for me.

 

In any case, for the official Dropbox Python SDK, you can find examples of getting and using a refresh token at the following links:
 

igor-plotnikov
New member | Level 2

Here is what I see:

 

Screenshot 2023-08-09 121746.png

 

 

Thank you for the useful links!

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    igor-plotnikov New member | Level 2
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    jgriffo1 New member | Level 2
What do Dropbox user levels mean?