Friday, May 18, 2012

A Dropbox client example in Python

I started to write a hobby program to work with Dropbox recently. While I was trying to write a program, I realized that my impatience was not helped by the documentation that is available on Dropbox.

While I was looking to get the program working for me, I did see that a lot of people were interested in using Dropbox as a file store for their applications and not really have each user sign-in and allow access to Dropbox. This is absolutely possible. There were other questions on how to use the access token and store it for future use. In this post, I answers the dummy's question - How can I use Dropbox with my Phyton program to store and distribute files that are not specific to a user's context.

I am assuming that the people reading this already understand virtualenv. If not, there is a very simple explanation on the flask site. Let's start by creating an environment for ourselves. Here is what I did:


Before we start, you should create an "App" in Dropbox. Start by going to https://www.dropbox.com/developers/apps. Create an App with any name you wish.


Notice that I left the access level to app folder. This will create a folder in Dropbox with the "App Name" that was provided. The program we write will have access to this folder ONLY.

Once you create the application, you'd notice that Dropbox creates a unique set of App key and App Secret. This is required to identify the application that accesses Dropbox. We will use it in our program.


Before we start writing our programs, we need to ensure we have the dropbox libraries. If you are using virtualenv as I did, this should be as simple as running pip


Dropbox uses OAuth. Therefore, each user will need to get a auth token to access Dropbox. The tokens need to be generated once and can be used repeatedly. Dropbox forums say that the token do not expire for a "long time". I believe that they live until the user revokes it. So if you are using one user to serve files, you have very little to worry about.

Here is a program that is adopted from the one on the Dropbox Authentication Tutorial. We generate a request token; wait for the user to authenticate and print the auth tokens for reference and future use.

# Include the Dropbox SDK libraries
from dropbox import client, rest, session

# Get your app key and secret from the Dropbox developer website
APP_KEY = '3w7xv4d9lrkc7c3'
APP_SECRET = '1v5f80mztbd3m9t'

# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
ACCESS_TYPE = 'app_folder'

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
access_token = sess.obtain_access_token(request_token)

#Print the token for future reference
print access_token

Be sure to change the APP_KEY and APP_SECRET to what you obtained during Setup. When you run the program, it should prompt you to visit a link using a browser. This will essentially let you authorize the application. When you follow the link, you should see:


Once you have allowed access to the application, the program will generate auth tokens.


Now that we have the token we can write a program that will put some files on Dropbox. Let's use the access token we generated and create a client that will upload a file:

# Include the Dropbox SDK libraries
from dropbox import client, rest, session

# Get your app key and secret from the Dropbox developer website
APP_KEY = '3w7xv4d9lrkc7c3'
APP_SECRET = '1v5f80mztbd3m9t'

# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
ACCESS_TYPE = 'app_folder'

sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)

# We will use the OAuth token we generated already. The set_token API 
# accepts the oauth_token and oauth_token_secret as inputs.
sess.set_token("cixkl1o65lo63rq", "ra6lnnkpq8d51cb")

# Create an instance of the dropbox client for the session. This is
# all we need to perform other actions
client = client.DropboxClient(sess)

# Let's upload a file!
f = open('dropbox-upload.py')
response = client.put_file('/dropbox-upload.py', f)
print "uploaded:", response

Note that the sequence of oauth_token and oauth_token_secret that is printed by the program and the one used by the set_token API are reversed. When executed, the source file for the program is uploaded to Dropbox.


Since we set the ACCESS_TYPE as app_foler, the file that is uploaded should be available under Apps in a folder that matches the name of the Application we created. Based on this example, the file should be available at https://www.dropbox.com/home/Apps/Taught%20Process


3 comments:

  1. When I do the 'print access_token', I get 'dropbox.session.OAuthToken object at 0x1102d4210'

    How do I get the actual token?

    thanks

    ReplyDelete
  2. @ze'ev:
    refer to http://stackoverflow.com/questions/10549326/python-dropbox-api-save-token-file

    ReplyDelete
  3. You have to save two keys from Access Token. One is 'Access Token Key' and other is 'Access Token Secret'.

    For printing these two keys -

    >>> print access_token.key
    >>> print access_token.secret

    ReplyDelete