def get_request_token(self):
 
        token_info = self.get_data_from_signed_url(
            self.service_info['request_token_url'], **self.request_params
            )
 
        token = OAuthRequestToken(
            service=self.service,
            **dict(token.split('=') for token in token_info.split('&'))
            )
 
        token.put()
 
        if self.oauth_callback:
            oauth_callback = {'oauth_callback': self.oauth_callback}
        else:
            oauth_callback = {}
            
        self.handler.redirect(self.get_signed_url(
            self.service_info['user_auth_url'], token, **oauth_callback
            ))
 def cleanup(self):
     query = OAuthRequestToken.all().filter(
         'created <', datetime.now() - EXPIRATION_WINDOW
         )
     token_count = query.count(CLEANUP_BATCH_SIZE)
     db.delete(query.fetch(CLEANUP_BATCH_SIZE))
     
     EXPIRATION_STATUS_CODES = timedelta(seconds=60*60*24*3) # 3 days
     query = OAuthStatusCodes.all().filter(
         'date <', datetime.now() - EXPIRATION_STATUS_CODES
         )
     codes_count = query.count(CLEANUP_BATCH_SIZE)
     db.delete(query.fetch(CLEANUP_BATCH_SIZE))
     return "Cleaned Token:%i Codes:%i entries" % (token_count, codes_count)
    def callback(self, return_to='/config'):
        oauth_token = self.handler.request.get("oauth_token")
 
        if not oauth_token:
            return self.get_request_token()
 
        oauth_token = OAuthRequestToken.all().filter(
            'oauth_token =', oauth_token).filter(
            'service =', self.service).fetch(1)[0]
 
        token_info = self.get_data_from_signed_url(
            self.service_info['access_token_url'], oauth_token
            )
        
        """
        key_name = create_uuid()

        self.token = OAuthAccessToken(
            key_name=key_name, service=self.service,
            **dict(token.split('=') for token in token_info.split('&'))
            )
 
        if 'specifier_handler' in self.service_info:
            specifier = self.token.specifier = self.service_info['specifier_handler'](self)
            old = OAuthAccessToken.all().filter(
                'specifier =', specifier).filter(
                'service =', self.service)
            db.delete(old)
 
        self.token.put()
        """
        
        #logging.error("token_info="+token_info)
        token_info_list = dict(token.split('=') for token in token_info.split('&'))
        
        token_query = OAuthAccessToken.all()
        token_query.filter("oauth_token =", token_info_list["oauth_token"])
        token_query.filter("oauth_token_secret =", token_info_list["oauth_token_secret"])
        token_query.filter("service =", self.service)
       
        #logging.error("oauth_token="+token_info_list["oauth_token"])
        #logging.error("service="+self.service)
        
        #remember_key = create_key(token_info_list["screen_name"])
        
        if not token_query.count():
            key_name = create_uuid()
            self.token = OAuthAccessToken(key_name=key_name)
            #self.token.key_name = create_uuid()
            self.token.oauth_token = token_info_list["oauth_token"]
            self.token.oauth_token_secret = token_info_list["oauth_token_secret"]
            self.token.service = self.service
            self.token.specifier = token_info_list["screen_name"]
            #self.token.remember_key = remember_key
            self.token.put()
            
        else:
            self.token = token_query.get()
            if self.token.specifier != token_info_list["screen_name"]:
                self.token.specifier = token_info_list["screen_name"]
                
            self.token.updated_at = datetime.now()
            self.token.put()
            
        user = users.get_current_user()
        user_prefs_query = UserPrefs.all()
        user_prefs_query.filter("google_id =", user)
        #user_prefs_query.filter("oauth_access_token_key =", self.token.key())
        user_prefs = user_prefs_query.get()
        if user_prefs is None:
            user_prefs = UserPrefs()
            user_prefs.google_id = user
            user_prefs.oauth_access_token_key = self.token.key()
            user_prefs.put()
        else:
            #user_prefs.google_id = user
            user_prefs.oauth_access_token_key = self.token.key()
            user_prefs.put()
        
        #cookie_value = {}
        #cookie_value['key'] = remember_key
        #cookie_value['specifier'] = self.token.specifier
        
        #memcache.add("twitter_token_" + remember_key, self.token, 3600)
        
        #self.set_cookie(cookie_value)
        self.handler.redirect(return_to)