Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
FrancoisNOYEZ
5 years agoExplorer | Level 4
How to migrate to new Auth workflow with python SDK while minimizing need for single user input?
Hello Dropbox team,
I have set up a small app as a python script which runs continuously on my server, and which needs to read some files that I store on my Dropbox storage space, in the folder...
- 5 years ago
FrancoisNOYEZ Здравко is correct, under the new system you do not need to interact with your script every time it runs. You can supply it the refresh token like you previously supplied it a long-lived access token and it will automatically run without manual user interaction, as before.
You do need to process the app authorization flow one time to get a refresh token though. You can refer to this example to see how to process the app authorization flow to get a refresh token, at which point you can then set up a client using it, as shown at the end of the example.
Greg-DB
Dropbox Community Moderator
3 years agotszikszai I see Здравко helpfully provided some sample code. For additional reference, you can find the documentation for the OAuth2FlowNoRedirectResult object here, which is returned by OAuth2FlowNoRedirectResult. There's also an example of how to get the refresh token in particular out of that object and pass it into a Dropbox client here.
tszikszai
3 years agoExplorer | Level 3
Thank you for all this!! The first part of the example works, however I am still struggling. I was able to get a REFRESH_TOKEN. However where I run into trouble is when I am trying to use the REFRESH_TOKEN to replace an expired ACCESS_TOKEN. So for example when I run this code and it shows that the ACCESS_TOKEN expired:
dbx = dropbox.Dropbox(ACCESS_TOKEN)
try:
dbx.users_get_current_account()
print('Dropbox connection successful')
except AuthError as e:
This throws an exception as my current ACCESS_TOKEN has expired. So I try to do this to replace it with a new one but it does not work:
auth_flow = dropbox.DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
try:
new_access_token, _ = auth_flow.refresh_access_token(REFRESH_TOKEN)
Or also tried this and this does not work either:
dbx = dropbox.Dropbox(APP_KEY, oauth2_access_token=ACCESS_TOKEN)
try:
# Use the refresh token to obtain a new access token
new_access_token, refresh_token = dbx.oauth2_token_refresh(REFRESH_TOKEN)
I keep getting exceptions and never able to replace the expired access token and get a new_access_token. What am I doing wrong? As I said the first part of the code worked, and I created a brand new app and went through the OAuth flow and have the APP_KEY, APP_SECRET and REFRESH_TOKEN, so I just don't see what I am doing wrong. I feel I am missing something fundamental here.
- Greg-DB3 years ago
Dropbox Community Moderator
tszikszai You need to supply the refresh token to the dropbox.Dropbox constructor so it can use it to perform the refresh automatically when needed. Please refer to the example of how to construct that with a refresh token here.
- tszikszai3 years agoExplorer | Level 3
I guess I am just trying to get to the next line of code of how to use this client to actually upload a file to Dropbox. Could you give me a code example?
- Greg-DB3 years ago
Dropbox Community Moderator
tszikszai Once you have a client constructed with a refresh token, you can use it to upload a file the same was as before, by calling files_upload.
Here's some simplified code from the example, combined with some of your uploading code, as an example:
import dropbox APP_KEY = "APPKEYEHERE" APP_SECRET = "APPSECRETHERE" REFRESH_TOKEN = "REFRESHTOKENHERE" dbx = dropbox.Dropbox(oauth2_refresh_token=REFRESH_TOKEN, app_key=APP_KEY, app_secret=APP_SECRET) local_file_path = "/local/path/here" dropbox_path = "/destination/path/here" with open(local_file_path, 'rb') as f: result = dbx.files_upload(f.read(), dropbox_path, mode=dropbox.files.WriteMode.overwrite) print(result) - tszikszai3 years agoExplorer | Level 3
Thank you!!!! Exactly what I was looking for!!
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!