cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and 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 to migrate to new Auth workflow with python SDK while minimizing need for single user input?

How to migrate to new Auth workflow with python SDK while minimizing need for single user input?

FrancoisNOYEZ
Explorer | Level 4
Go to solution

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 dedicated to this app. This script needs to read those files at regular interval (currently every 15 minutes). When I set up this app, I generated a token that I now provide to my python script every time I need to launch it. Things have been working fine up until now.

 

I have been warned of the API update, notably related to authentication, by an email in November 2020.
Now I'm tring to see if I need to change something to my app in order to adapt to this update, and if yes, to implement that change.
However it has been difficult to find out if I need to do something, and if yes, what.
If I understand properly what I read on this page (https://developers.dropbox.com/fr-fr/oauth-guide), as long as only me is using this app, I can use a token generated in the OAuth 2 Section of the Setting menu of my app, and set its expiration setting to 'No expiration'. However, in the help box, it is said that such tokens will be deprecated in the future, so if possible I'd like to anticipate that.
When I look at the table at the end of this page, in the "Summary" section, if I parse it properly, my app falls within the "A server-side application that requires background access." category, which means what I should "Use the OAuth code flow, with refresh tokens.".
The thing is, I'm not sure of how to do that. When I look at examples available here (https://www.dropbox.com/developers/reference/getting-started?_tk=guides_lp&_ad=tutorial5&_camp=get_s...), seems like it's just using the long-lived token method that I'm already using.
I found some examples script for the python SDK here (https://github.com/dropbox/dropbox-sdk-python/tree/main/example/oauth) but I'm not sure which one to follow.
From what I can see, I will always need to interact with the script every time I launch it, in order to copy the authorization code and provide it to the script, is that right?

 

Basically my question is: what is the script setup that I need to do, that will minimize the interaction with Dropbox required from me, while making sure that my app is still functional and compliant with Dropbox's future deprecation, for the time being?

13 Replies 13

Greg-DB
Dropbox Staff
Go to solution

@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.

tszikszai
Explorer | Level 3
Go to solution

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-DB
Dropbox Staff
Go to solution

@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)

 

tszikszai
Explorer | Level 3
Go to solution

Thank you!!!!  Exactly what I was looking for!!

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    tszikszai Explorer | Level 3
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?