Created
December 20, 2012 04:44
-
-
Save brianpartridge/4343011 to your computer and use it in GitHub Desktop.
Dropbox - Connect
This file contains 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
import json | |
from dropbox import client, rest, session | |
import webbrowser | |
import keychain | |
import sys | |
import os | |
### Config | |
config = json.load(open('config.json', 'r')) | |
SERVICE = 'dropbox' | |
APP_KEY = config['dropbox-app-key'] | |
APP_SECRET = config['dropbox-app-secret'] | |
ACCESS_TYPE = 'dropbox' | |
### Functions | |
def setToken(session): | |
key = keychain.get_password(SERVICE, 'key') | |
secret = keychain.get_password(SERVICE, 'secret') | |
if not key or not secret: | |
key, secret = getToken(session) | |
keychain.set_password(SERVICE, 'key', key) | |
keychain.set_password(SERVICE, 'secret', secret) | |
session.set_token(key, secret) | |
def getToken(session): | |
print "Authorization required " | |
request_token = sess.obtain_request_token() | |
url = sess.build_authorize_url(request_token) | |
# Open the browser modally to allow access and block until the user is done. | |
webbrowser.open(url, modal=True) | |
# Now, verify that access was granted. | |
access_token = sess.obtain_access_token(request_token) | |
return access_token.key, access_token.secret | |
### Logic | |
path, script = os.path.split(sys.argv[0]) | |
name, ext = os.path.splitext(script) | |
print "### %s ### " % name | |
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE) | |
setToken(sess) | |
client = client.DropboxClient(sess) | |
print "Dropbox account:", client.account_info()['email'] | |
print "### Done ###" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment