Created
December 2, 2012 14:02
-
-
Save bhutley/4188828 to your computer and use it in GitHub Desktop.
Initialising a python script to access Dropbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # | |
| # This is a simple script for setting up the dropbox api. | |
| # It can be used as the basis for a more complex script. | |
| # The file ~/.dropbox.cfg is where you store the oauth authorisation | |
| # tokens. It has the following format: | |
| # | |
| # | |
| # [AppName] | |
| # | |
| # app_key = <XYZZY> | |
| # app_secret = <XYZZY> | |
| # | |
| # access_key = <XYZZY> | |
| # access_secret = <XYZZY> | |
| # | |
| # You shouldn't define the access_key or access_secret values | |
| # initially, the script will prompt the user to visit the dropbox | |
| # website in order to authorise the app, and then write the values | |
| # to the file. | |
| # | |
| # This was written quickly to start using the Dropbox API in a simple | |
| # script. I'm putting it on Github so I can refer to it in future. | |
| # | |
| # Brett Hutley <brett@hutley.net> | |
| import sys, os | |
| from ConfigParser import SafeConfigParser | |
| from optparse import OptionParser | |
| from dropbox import client, rest, session | |
| optp = OptionParser() | |
| optp.add_option("-c", "--config", dest="config_file", metavar="FILE", | |
| default= os.path.join(os.path.expanduser("~"), ".dropbox.cfg"), | |
| help="dropbox configuration file") | |
| optp.add_option("-a", "--app", dest="app", default="MyApp", | |
| help="Application config to use.") | |
| opts, args = optp.parse_args() | |
| if not os.path.exists(opts.config_file): | |
| error("config file '%s' doesn't exist" % opts.config_file) | |
| exit(1) | |
| config_parser = SafeConfigParser() | |
| config_parser.read(opts.config_file) | |
| app_key = config_parser.get(opts.app, 'app_key') | |
| app_secret = config_parser.get(opts.app, 'app_secret') | |
| access_type = 'dropbox' # should be 'dropbox' or 'app_folder' as configured for your app | |
| sess = session.DropboxSession(app_key, app_secret, access_type) | |
| access_key = None | |
| access_secret = None | |
| try: | |
| access_key = config_parser.get(opts.app, 'access_key') | |
| access_secret = config_parser.get(opts.app, 'access_secret') | |
| except: | |
| pass | |
| if access_key is None or access_secret is None: | |
| request_token = sess.obtain_request_token() | |
| url = sess.build_authorize_url(request_token) | |
| print "url:", url | |
| print "Please visit this website and press the 'Allow' button, then hit 'Enter' here." | |
| raw_input() | |
| access_token = sess.obtain_access_token(request_token) | |
| access_key = access_token.key | |
| access_secret = access_token.secret | |
| cfg_file = open(opts.config_file, 'a+') | |
| cfg_file.write("access_key = %s\n" % (access_key, )) | |
| cfg_file.write("access_secret = %s\n" % (access_secret, )) | |
| cfg_file.close() | |
| sess.set_token(access_key, access_secret) | |
| client = client.DropboxClient(sess) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment