Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# Go to to create an app.
app_key = 'YOUR_APP_KEY'
app_secret = 'YOUR_APP_SECRET'
# access_type can be 'app_folder' or 'dropbox', depending on
# how you registered your app.
access_type = 'app_folder'
import webbrowser
from dropbox import client, rest, session
import keychain
import pickle
import console
def get_request_token():
print 'Getting request token...'
sess = session.DropboxSession(app_key, app_secret, access_type)
request_token = sess.obtain_request_token()
url = sess.build_authorize_url(request_token)
console.clear(), modal=True)
return request_token
def get_access_token():
token_str = keychain.get_password('dropbox', app_key)
if token_str:
key, secret = pickle.loads(token_str)
return session.OAuthToken(key, secret)
request_token = get_request_token()
sess = session.DropboxSession(app_key, app_secret, access_type)
access_token = sess.obtain_access_token(request_token)
token_str = pickle.dumps((access_token.key, access_token.secret))
keychain.set_password('dropbox', app_key, token_str)
return access_token
def get_client():
access_token = get_access_token()
sess = session.DropboxSession(app_key, app_secret, access_type)
sess.set_token(access_token.key, access_token.secret)
dropbox_client = client.DropboxClient(sess)
return dropbox_client
def main():
# Demo if started run as a script...
# Just print the account info to verify that the authentication worked:
print 'Getting account info...'
dropbox_client = get_client()
account_info = dropbox_client.account_info()
print 'linked account:', account_info
if __name__ == '__main__':

This comment has been minimized.

Copy link

inactivist commented Sep 22, 2013

Greetings, and thanks for this gist.

I'm trying to use this with the example... pardon my ignorance, but where may I find the 'keychain' module? I've searched but can't seem to locate it in any of the usual ways.

$ python 
Traceback (most recent call last):
  File "", line 8, in <module>
    import dropboxlogin # this code can be found here
  File "/home/inactivist/workspace/dropbox-sync/", line 13, in <module>
    import keychain
ImportError: No module named keychain

(Python 2.7.3, Ubuntu 12.04 X64)


This comment has been minimized.

Copy link

TerryLansdown commented Jun 15, 2016

Update: A bit more reading and I fixed my problem by adding
#! python2 to the start of the

Hi, just upgraded to Pythonista v3 (many thanks, it's great so far!); fixed the print(statement)s but when I run this script (which has been working fine for several years) I'm getting a [401] 'Request token not found.' The app goes to the browser to authenticate with DB but seems to stall. Any ideas? Thanks.


This comment has been minimized.

Copy link

cclauss commented Aug 22, 2016

There is a Dropbox API v2 version that is compatible with Pythonista 3 discussed at

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.