Пример #1
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)
Пример #2
0
 def __init__(self):
     session = DropboxSession(settings.DROPBOX_CONSUMER_KEY,
                              settings.DROPBOX_CONSUMER_SECRET,
                              settings.DROPBOX_ACCESS_TYPE,
                              locale=None)
     session.set_token(settings.DROPBOX_ACCESS_TOKEN,
                       settings.DROPBOX_ACCESS_TOKEN_SECRET)
     self.client = DropboxClient(session)
Пример #3
0
def _create_generic_session(rest_client,
                            consumer_key='a',
                            consumer_secret='b',
                            access_type='app_folder'):
    return DropboxSession(consumer_key,
                          consumer_secret,
                          access_type,
                          rest_client=rest_client)
Пример #4
0
 def session(self):
     """
     Initialize or return already initialized ``DropboxSession`` instance.
     """
     if not hasattr(self.cache_storage, SESSION_CACHE_KEY):
         session = DropboxSession(self.DROPBOX_KEY, self.DROPBOX_SECRET,
                                  self.DROPBOX_ACCESS_TYPE)
         setattr(self.cache_storage, SESSION_CACHE_KEY, session)
     return getattr(self.cache_storage, SESSION_CACHE_KEY)
Пример #5
0
 def __init__(self, location='/Public'):
     session = DropboxSession(CONSUMER_KEY,
                              CONSUMER_SECRET,
                              ACCESS_TYPE,
                              locale=None)
     session.set_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
     self.client = DropboxClient(session)
     self.account_info = self.client.account_info()
     self.location = location
     self.base_url = 'http://dl.dropbox.com/u/{uid}/'.format(
         **self.account_info)
Пример #6
0
 def __init__(self, file, name=None):
     self.dropbox_metadata = file
     name = name or os.path.basename(self.dropbox_metadata["path"])
     super(DropboxFile, self).__init__(file, name=name)
     self._size = self.dropbox_metadata["size"]
     access_type = getattr(settings, "DROPBOX_ACCESS_TYPE", "app_folder")
     dropbox_session = DropboxSession(settings.DROPBOX_APP_KEY,
                                      settings.DROPBOX_APP_SECRET_KEY,
                                      access_type)
     dropbox_session.set_token(settings.DROPBOX_APP_ACCESS_TOKEN,
                               settings.DROPBOX_APP_ACCESS_TOKEN_SECRET)
     self.dropbox_client = DropboxClient(dropbox_session)
Пример #7
0
 def __init__(self, *args, **kwargs):
     super(DropboxFileUploadHandler, self).__init__(*args, **kwargs)
     fallback_dropbox_folder = getattr(settings,
                                       "DROPBOX_FILE_UPLOAD_FOLDER", "/")
     self.dropbox_folder = kwargs.get("dropbox_folder",
                                      fallback_dropbox_folder)
     access_type = getattr(settings, "DROPBOX_ACCESS_TYPE", "app_folder")
     dropbox_session = DropboxSession(settings.DROPBOX_APP_KEY,
                                      settings.DROPBOX_APP_SECRET_KEY,
                                      access_type)
     dropbox_session.set_token(settings.DROPBOX_APP_ACCESS_TOKEN,
                               settings.DROPBOX_APP_ACCESS_TOKEN_SECRET)
     self.dropbox_client = DropboxClient(dropbox_session)
Пример #8
0
    def get_userinfo(self):
        """
        {u'referral_link': u'https://db.tt/LbG4aSx1', u'display_name': u'waiting easilydo', u'uid': 330936854, u'country': u'HK', u'email': u'*****@*****.**', u'team': None, u'quota_info': {u'datastores': 0, u'shared': 0, u'quota': 2147483648, u'normal': 363213}}
        """
        sess = DropboxSession(self.client_id, self.client_secret, self.ACCESS_TYPE)
        sess.set_token(self.token['oauth_token'], self.token['oauth_token_secret'])

        api = DropboxClient(sess)
        try:
            res = api.account_info()
            return res
        except Exception, e:
            logging.warning("dropbox query info fail %s", e, exc_info=True)
            return None
Пример #9
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
Пример #10
0
def connect_services():
    g.dropbox = DropboxSession(app.config['DROPBOX_APP_KEY'],
                               app.config['DROPBOX_SECRET'], 'app_folder')