Dropbox module and access token
I am creating some scripts for my use only that will hopefully be able to use the Dropbox module that is included with Pythonista.
Since the scripts are private, I would like to use the generated Dropbox access token instead of the app key/secret so that I don't need to go through the login authentication process each time.
Is there any way to use the access token with the very convenient Dropbox module? The example uses app key/secret and I've not been able to figure out how to use the access token instead. Please help?
[By the way, I have been loving Pythonista on my iPad--what a great tool and editor. Thank you again!]
SimCityWok last edited by
I'm curious to know this too.
I have lost my 2FA device for my Dropbox account and the only way I can see gaining access is using the Pythonista Dropbox API. I have 2 sets of secret/keys and also the longer tokens. ±However as OP says, with the secret/key, one still needs to authenticate using login (and 2fa).
Am I wasting my time trying to access Dropbox using Pythonista even with the long token string? I am 99% certain the API keys were activated at the time
what code are you using to set up your dropbox session? Skimming through the source, there are ways to set the token.
bennr01 last edited by bennr01
If you just want to skip the authentication process and can use the appkey and appsecret, you can save and load the token. I wrote a script using this functionality for StaSh. See https://github.com/ywangd/stash/blob/master/bin/mc.py (the get_dropbox_client() function in line 558). The script contains some bugs, but im working on fixing it.
@JonB l am following the method used in the example supplied with the Pythonista onboard documentation. In Console click on the circled ? to Pythonista Documentation > Pythonista Modules > Dropbox which will have the section called Dropbox Client. The session code from the example there looks like this:
from dropbox import client, rest, session APP_KEY = 'my_key_is_here' APP_SECRET = 'my_secret_is_here' ACCESS_TYPE = 'dropbox' sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE) request_token = sess.obtain_request_token() url = sess.build_authorize_url(request_token) # Make the user sign in and authorize this token print "url:", url print "Please visit this website and press the 'Allow' button, then hit 'Enter' here." raw_input() # This will fail if the user didn't visit the above URL and hit 'Allow' access_token = sess.obtain_access_token(request_token) client = client.DropboxClient(sess) print "linked account:", client.account_info()
The URL can be used to log in as a user to Dropbox.
I hope this is what you were asking for.
Is there a way to rewire this to use the generated access token and avoid the user authentication step? That would make my private scripts log in seamlessly and still use the features of the Dropbox module.
The DropboxSession (sess in the code above) object has a set_token() method used as:
which says it will attach an access token to the DropboxSession. That sounds perfect, except the Dropbox app page only generates an access token and says nothing about an access secret.
The generated access token I have is 64 characters long. After the first 32 characters there is an underscore which is then followed by 31 more characters.
Can my access token be used somehow with the set_token method?
Dropbox is migrating to SDK API v2. The Dropbox module in Pythonista is for SDK API v1. That probably explains the disconnect between my token and the API I am referencing.
I was able to get an access key and secret by running the example script and logging in and printing them (access_token.key and access_token.secret). Using these with the set_token() method and removing all the authentication stuff works just fine. These v1 key and secret strings are 16 and 15 in length.
How do I put in an official request to include Dropbox SDK API v2 in Pythonista? It's a little ugly as both versions need to be around during transition time.
I could be wrong, but this is just the official dropbox python sdk, which omz shipped with pythonista. In the beta,
dropbox.__version__is 6.4, which is the latest. This supports both v1 and v2 api, with different methods. I suspect the example code you are using is outdated... You can use v2 with:
instead of dropbox.client.Dropbox (which is v1)
What version do you have? If that syntax does not work, you can use stash to
pip import dropbox
@JonB Thank you for your attempts to help, and I apologize if I didn't explain things clearly. I am running the latest version of Pythonista, which I think is only for iOS devices. I am using an iPad. I have no idea what version of the Dropbox SDK module I have, as
dropbox.__version__does not exist. I have whatever was packaged with Pythonista. From the method calls it is clearly API v1, but beyond that I cannot tell you.
I think the example code is correct for the version it is packaged with. It is right out of the Pythonista documentation. The newer method for which you gave an example does not exist in this version. Back to my original question, I think the 64-character access key generated by Dropbox is not of any use to me here. But what I captured after my login seems to work.
stash? pip? Thank you for the suggestion, but I only have an iPad anymore.
Thanks again, truly. I'll try to keep going on my project with whatever I have in hand.
ccc last edited by ccc
import dropbox print(dropbox.__version__)
This has worked on all Pythonista versions that have dropbox. See: https://github.com/cclauss/pythonista-module-versions
Okay, sorry, I forget that not everyone knows about or uses stash. See the readme at that link for a one liner that you can copy/paste into the pythonista console, which will install it. StaSH is a commandline shell, which adds several features to pythonista, one of the very useful ones is the pip command, which lets you install many third party python packages.
Install StaSH, restart pythonista, then run the launch_stash.py which appears in your documents folder. Then, type
pip install dropbox
which will install the latest released dropbox sdk into your site-packages folder. Thereafter, when you use import dropbox, it will use the updated package, and you can use the new syntax.
Incidentally, @omz, did you update
dropboxin the pythonista 2.1 hitting the app store soon? 201009 still has the old version.
@ccc check again, in 2.0 there is no version (pythonista_versions returns ????)
@JonB Thank you so much for the stash information. Now your reply makes total sense. Yes, not everyone finds all the little Pythonista nuggets. Just have to keep turning over the rocks. Thanks again.
@SimCityWok Since you have your access token you should be in good shape, especially with Pythonista 2.1.
Take a look at Dropbox API Explorer in the Dropbox developer's area. It allows you to play and access your dropbox and generate code with just your token in hand.
Good luck -- I hope you can get in again.