We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
smitpatel
2 years agoNew member | Level 2
Regarding the automatic refreshing of the access token
def initialize_dropbox_client():
access_token = "my access token"
dbx = dropbox.Dropbox(access_token)
dbx._oauth2_access_token_expiration = time.time() + 14400 # Assume token expires in 1 hour for initial setup
return dbx
# Refresh access token if it's expired
def refresh_access_token():
global dbx # Ensure dbx is accessed as a global variable
client_id = "app id"
client_secret = "app secret"
refresh_token = "resfresh token"
while True:
try:
current_time = time.time()
expiration_time = dbx._oauth2_access_token_expiration
if expiration_time is not None and expiration_time - current_time < 60: # If token expires in less than a minute
data = {
"refresh_token": refresh_token,
"grant_type": "refresh_token",
"client_id": client_id,
"client_secret": client_secret,
}
response = requests.post(token_url, data=data)
new_tokens = response.json()
new_access_token = new_tokens["access_token"]
dbx = dropbox.Dropbox(new_access_token)
dbx._oauth2_access_token_expiration = current_time + new_tokens["expires_in"]
else:
time.sleep(60) # Check again after 1 minute
except dropbox.exceptions.AuthError:
st.error("Authentication error. Please check your access token.")
break
i buid auto refreshing of the access token. but it is not automatically refreshing to the new access token and instead of that it is showing an auth error
1 Reply
- Greg-DB2 years ago
Dropbox Community Moderator
I see you're using the Dropbox Python SDK and are trying to implement the refresh process yourself. Please note that the SDK can handle the refresh functionality for you automatically, so you don't need to implement that yourself.
As long as you provide all of the relevant credentials to the 'Dropbox' object like in the example here, it will automatically handle the refresh process for you whenever needed.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!