Ejemplo n.º 1
0
class DropboxAPIFacade(object):
	is_setup = False

	"""Easy to use facade for the Dropbox API and database

	Provides full functionality to use the Dropbox API. It saves and retrieves
	access/request tokens automatically from the database. The API session gets
	fed with the right data from the database when it needs it. 

	"""

	def __init__(self, autostart):
		"""Creates the instance and sets up all data and session if autostart"""
		if autostart: self.setup()

	def setup(self):
		"""Retrieves all startup data and creates a new DropboxClient with it"""

		(access_key, access_secret) = dropbox_access.get_access_token()
		self.session = DropboxSession(DROPBOX_APP_KEY, DROPBOX_APP_SECRET, DROPBOX_ACCESS_TYPE)
		self.session.obtain_request_token()
		self.session.set_token(access_key, access_secret)
		self.client = DropboxClient(self.session)
		self.is_setup = True

	def get_account(self):
		"""Retrieves the account information from the Dropbox API"""

		try:
			return self.client.account_info()
		except ErrorResponse, e:
			return None
Ejemplo n.º 2
0
def connectDropbox():
    sess = DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)

    if os.path.exists(TOKENS):
        token_file = open(TOKENS)
        token_key, token_secret = token_file.read().split('|')
        token_file.close()
        sess.set_token(token_key, token_secret)
    else:
        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)

        # Save the key to the file so we don't need to do this again
        token_file = open(TOKENS, 'w')

        token_key = access_token.key
        token_secret = access_token.secret
        token_file.write("%s|%s" % (token_key, token_secret))

        token_file.close()

    client = DropboxClient(sess)
    print "Linked account: %s" % client.account_info()
    return client
Ejemplo n.º 3
0
 def connect(self, query):
     sess = DropboxSession(self.app_key, self.app_secret, "app_folder")
     # Access token is saved to memcache and the filesystem
     s_token = self.cache.get("s_token") or read_file(".s_token")
     s_secret = self.cache.get("s_secret") or read_file(".s_secret")
     if s_token and s_secret:
         sess.set_token(s_token, s_secret)
     elif "oauth_token" in query:  # callback from Dropbox
         s_token = sess.obtain_access_token(
             dropbox.session.OAuthToken(self.cache.get("r_token"),
                                        self.cache.get("r_token_secret")))
         self.cache.set("s_token", s_token.key)
         self.cache.set("s_secret", s_token.secret)
         with open(".s_token", "w") as f:
             f.write(s_token.key)
         with open(".s_secret", "w") as f:
             f.write(s_token.secret)
         self.cache.delete("r_token")
         self.cache.delete("r_token_secret")
     else:  # start of Dropbox auth
         req_token = sess.obtain_request_token()
         self.cache.set("r_token", req_token.key)
         self.cache.set("r_token_secret", req_token.secret)
         url = sess.build_authorize_url(req_token, cherrypy.url())
         raise cherrypy.HTTPRedirect(url)
     self.client = DropboxClient(sess)
Ejemplo n.º 4
0
 def connect(self, query):
     sess = DropboxSession(self.app_key, self.app_secret, "app_folder")
     # Access token is saved to memcache and the filesystem
     s_token = self.cache.get("s_token") or read_file(".s_token")
     s_secret = self.cache.get("s_secret") or read_file(".s_secret")
     if s_token and s_secret:
         sess.set_token(s_token, s_secret)
     elif "oauth_token" in query:  # callback from Dropbox
         s_token = sess.obtain_access_token(dropbox.session.OAuthToken(
             self.cache.get("r_token"), self.cache.get("r_token_secret")))
         self.cache.set("s_token", s_token.key)
         self.cache.set("s_secret", s_token.secret)
         with open(".s_token", "w") as f:
             f.write(s_token.key)
         with open(".s_secret", "w") as f:
             f.write(s_token.secret)
         self.cache.delete("r_token")
         self.cache.delete("r_token_secret")
     else:  # start of Dropbox auth
         req_token = sess.obtain_request_token()
         self.cache.set("r_token", req_token.key)
         self.cache.set("r_token_secret", req_token.secret)
         url = sess.build_authorize_url(req_token, cherrypy.url())
         raise cherrypy.HTTPRedirect(url)
     self.client = DropboxClient(sess)
Ejemplo n.º 5
0
    def __init__(self, in_user_mode=True):
        """Connect to the Dropbox servers so that files can be uploaded"""

        self.m = Message(in_user_mode=in_user_mode)

        session = DropboxSession(API_KEY, API_SECRET, self.ACCESS_TYPE)

        token_file = PyJson(TOKEN_PATH)
        token = token_file.doc

        # If there is a token saved, that can be used to connect with Dropbox
        if 'key' in token and 'secret' in token:
            # Read the token and authenticate the session with it
            session.set_token(token['key'], token['secret'])

        # Otherwise it is necessary to authenticate for the first time
        else:
            # Request an access token
            request_token = session.obtain_request_token()
            url = session.build_authorize_url(request_token)

            # Open the authentication page in the user's browser and wait for them to accept
            webbrowser.open(url, new=2)

            print "Press enter once you have authorised the app in your browser"
            raw_input()

            # Get a new access token from the authenticated session
            # This will fail if the user didn't visit the above URL and press 'Allow'
            try:
                access_token = session.obtain_access_token(request_token)

            except Exception as error:
                if DEBUG:
                    print error
                print "You didn't authorise the app, or something else went wrong"
                exit(1)

            # Save the access token to a file so that authentication is not needed next time the app is run
            token_file.add('key', access_token.key)
            token_file.add('secret', access_token.secret)
            token_file.save()

        # Create a Dropbox client from the session
        client = DropboxClient(session)

        self.client = client
Ejemplo n.º 6
0
    def __init__(self, in_user_mode=True):
        """Connect to the Dropbox servers so that files can be uploaded"""

        self.m = Message(in_user_mode=in_user_mode)

        session = DropboxSession(API_KEY, API_SECRET, self.ACCESS_TYPE)

        token_file = PyJson(TOKEN_PATH)
        token = token_file.doc

        # If there is a token saved, that can be used to connect with Dropbox
        if 'key' in token and 'secret' in token:
            # Read the token and authenticate the session with it
            session.set_token(token['key'], token['secret'])

        # Otherwise it is necessary to authenticate for the first time
        else:
            # Request an access token
            request_token = session.obtain_request_token()
            url = session.build_authorize_url(request_token)

            # Open the authentication page in the user's browser and wait for them to accept
            webbrowser.open(url, new=2)

            print("Press enter once you have authorised the app in your browser")
            input()

            # Get a new access token from the authenticated session
            # This will fail if the user didn't visit the above URL and press 'Allow'
            try:
                access_token = session.obtain_access_token(request_token)

            except Exception as error:
                if DEBUG:
                    print(error)
                print("You didn't authorise the app, or something else went wrong")
                exit(1)

            # Save the access token to a file so that authentication is not needed next time the app is run
            token_file.add('key', access_token.key)
            token_file.add('secret', access_token.secret)
            token_file.save()

        # Create a Dropbox client from the session
        client = DropboxClient(session)

        self.client = client
Ejemplo n.º 7
0
import sys
import os.path
from dropbox.session import DropboxSession


if len(sys.argv) < 2:
    print("usage: %s <dropbox-api-key> <dropbox-api-secret> <output-file-name>\n" % os.path.basename(__file__))
    exit('not enough arguments')

app_key = sys.argv[1]
app_secret = sys.argv[2]
# should be 'dropbox' or 'app_folder' as configured for your app
access_type = 'app_folder'

file_name = sys.argv[3]

session = DropboxSession(app_key, app_secret, access_type)
request_token = session.obtain_request_token()
url = session.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 = session.obtain_access_token(request_token)

lines = [app_key, app_secret, access_token.key, access_token.secret]
with open(file_name, 'w') as f:
    f.write("\n".join(lines))

print('done')