cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: 

Re: Access Token Can't be refreshed without refresh token and app key in Python

Access Token Can't be refreshed without refresh token and app key in Python

Sufiyanashraf
Explorer | Level 3

I am building an app in python to automatically update a file using dropbox API, but it shows an error after 4 hours however I can't find any mistake can you point out my mistake? My code Link: https://github.com/sufiyanashraf/dropboxhelp/blob/main/cantrefresh

 

10 Replies 10

Greg-DB
Dropbox Staff

I see you are using the official Dropbox Python SDK to do the upload work, but are attempting to perform the refresh process yourself using 'requests' to call the /oauth2/token endpoint directly. The official Dropbox Python SDK can handle the refresh process for you automatically though, and I strongly recommend having it do so instead of attempting to implement the refresh process yourself. You can see an example of supplying the credentials to the Dropbox object here, in which case it will handle the refresh for you automatically.

Sufiyanashraf
Explorer | Level 3

Yes, but it is just setup nothing related to token refresh. basically, my software stopped after 4 hours first it showed the error of an expired access token. So, I used this method by searching. Can you please point out the corrections in my code?

Greg-DB
Dropbox Staff

In order to get this working, please set up the client by supplying the refresh token, etc., to it directly, as shown in the example, so that it can perform the refresh process for you. That way, it can continue performing API calls successfully for more than four hours.

Sufiyanashraf
Explorer | Level 3

So flow like this? It still shows an error. 

import sys
import dropbox
import time
from dropbox import DropboxOAuth2FlowNoRedirect
from dropbox.files import WriteMode

while True:
    with dropbox.Dropbox(oauth2_access_token='access token',
                     oauth2_access_token_expiration=expiry,
                     oauth2_refresh_token='refresh token',
                     app_key='app key',
                     app_secret='secret'):
        try:
            with open('rtk.txt', 'rb') as f:
                file_contents = f.read()   
            dbx.files_upload(file_contents, '/rtk.txt', mode=dropbox.files.WriteMode('overwrite'))
        except Exception as e:
            print(f"An error occurred: {e}")
    time.sleep(60)

 Error: An error occurred: name 'dbx' is not defined

Greg-DB
Dropbox Staff

That error indicates that you're attempting to use an object that isn't defined. Since you're trying to access a client with the name "dbx", add "as dbx" at the end of the "with" statement (before the ":"), like this:

    with dropbox.Dropbox(oauth2_access_token='access token',
                     oauth2_access_token_expiration=expiry,
                     oauth2_refresh_token='refresh token',
                     app_key='app key',
                     app_secret='secret') as dbx:

 (Or, you can still do it the other way, like "dbx = dropbox.Dropbox(...)" as long as you fill in credentials like above.)

Sufiyanashraf
Explorer | Level 3

I corrected but again error

import sys
import dropbox
from dropbox import DropboxOAuth2FlowNoRedirect
from dropbox.files import WriteMode
import time

while True:
    with dropbox.Dropbox(oauth2_access_token='sl access token',
                     oauth2_access_token_expiration=14400,
                     oauth2_refresh_token='refresh token',
                     app_key='app key',
                     app_secret='app secret') as dbx:
        try:
            with open('rtk.txt', 'rb') as f:
                file_contents = f.read()   
            dbx.files_upload(file_contents, '/rtk.txt', mode=dropbox.files.WriteMode('overwrite'))
        except Exception as e:
            print(f"An error occurred: {e}")
    time.sleep(60)

Showing error: "An error occurred: '>=' not supported between instances of 'datetime.datetime' and 'int'"

Greg-DB
Dropbox Staff

This error is indicating that oauth2_access_token_expiration requires a datetime, but you're now passing in an int. You should pass in a datetime for that. Or, you can just omit oauth2_access_token_expiration entirely and the SDK will just handle it as needed.

Sufiyanashraf
Explorer | Level 3

Remove this line? 

oauth2_access_token_expiration=14400,

Greg-DB
Dropbox Staff

Yes, either remove it or supply the correct type (datetime).

Need more support?