def __init__(self, twitter_config, urlToken=None): """ Initialise an instance of the TwitterAuth class that will enable us to create an oauth access token with twitter """ # initialise the configuration self.config = twitter_config self.requestToken = urlToken self.accessToken = None # create the consumer self.consumer = oauth.OAuthConsumer(self.config.consumerKey, self.config.consumerSecret) # if the oauth token is not set, then prepare the request token and authorization steps if self.requestToken is None: # create the request token self.requestToken = self._initRequestToken(URL_REQUEST_TOKEN) # authorize the request token self._authorizeToken() # otherwise, create an oauth token from the string passed to the function else: # look for the twitter user from the database requestKey = OAuthAccessKey.findByRequestKey(self.requestToken) # if we found the user if requestKey is not None: self.accessToken = oauth.OAuthToken.from_string( requestKey.requestKeyEncoded)
def __init__(self, twitter_config, urlToken = None): """ Initialise an instance of the TwitterAuth class that will enable us to create an oauth access token with twitter """ # initialise the configuration self.config = twitter_config self.requestToken = urlToken self.accessToken = None # create the consumer self.consumer = oauth.OAuthConsumer(self.config.consumerKey, self.config.consumerSecret) # if the oauth token is not set, then prepare the request token and authorization steps if self.requestToken is None: # create the request token self.requestToken = self._initRequestToken(URL_REQUEST_TOKEN) # authorize the request token self._authorizeToken() # otherwise, create an oauth token from the string passed to the function else: # look for the twitter user from the database requestKey = OAuthAccessKey.findByRequestKey(self.requestToken) # if we found the user if requestKey is not None: self.accessToken = oauth.OAuthToken.from_string(requestKey.requestKeyEncoded)
def processResponse(self, content, responseCallback): """ This method is used to process the response from twitter in the case that our request has been successful @content - the content of the response returned from the request @responseCallback - a method callback that can be used to push details back to the calling method """ # decode the json response searchResults = simplejson.loads(content) # if we have have a url auth token, then update the oauth details if self.urlAuthToken: auth_data = OAuthAccessKey.findByRequestKey(self.urlAuthToken) auth_data.userId = searchResults.get('id') auth_data.userName = searchResults.get('screen_name') auth_data.put() # read the unique numeric id of the user from twitter self.twitterId = searchResults.get('id', self.twitterId) # read the current screen name (this can change) self.screenName = searchResults.get('screen_name', self.screenName) # read what they have specified as their contact name self.realName = searchResults.get('name', self.realName) # read the url for the current avatar image self.profileImageUrl = searchResults.get('profile_image_url', self.profileImageUrl) # now get the bigger image self.profileImageUrl = self.profileImageUrl.replace( "_normal.jpg", "_bigger.jpg") # read the location they have entered into their profile - may or may not be useful self.location = searchResults.get('location', self.location) # read the current number of followers that user has self.followersCount = searchResults.get('followersCount', self.followersCount) # read the date at which they signed up for twitter self.dateCreated = searchResults.get('created_at', self.followersCount) # read the utc offset they have specified for their account (in seconds) self.utcOffset = searchResults.get('utc_offset', self.utcOffset) logging.info("Successfully logged in %s (%d: %s from %s)", self.screenName, self.twitterId, self.realName, self.location)
def processResponse(self, content, responseCallback): """ This method is used to process the response from twitter in the case that our request has been successful @content - the content of the response returned from the request @responseCallback - a method callback that can be used to push details back to the calling method """ # decode the json response searchResults = simplejson.loads(content) # if we have have a url auth token, then update the oauth details if self.urlAuthToken: auth_data = OAuthAccessKey.findByRequestKey(self.urlAuthToken) auth_data.userId = searchResults.get('id') auth_data.userName = searchResults.get('screen_name') auth_data.put() # read the unique numeric id of the user from twitter self.twitterId = searchResults.get('id', self.twitterId) # read the current screen name (this can change) self.screenName = searchResults.get('screen_name', self.screenName) # read what they have specified as their contact name self.realName = searchResults.get('name', self.realName) # read the url for the current avatar image self.profileImageUrl = searchResults.get('profile_image_url', self.profileImageUrl) # now get the bigger image self.profileImageUrl = self.profileImageUrl.replace("_normal.jpg", "_bigger.jpg") # read the location they have entered into their profile - may or may not be useful self.location = searchResults.get('location', self.location) # read the current number of followers that user has self.followersCount = searchResults.get('followersCount', self.followersCount) # read the date at which they signed up for twitter self.dateCreated = searchResults.get('created_at', self.followersCount) # read the utc offset they have specified for their account (in seconds) self.utcOffset = searchResults.get('utc_offset', self.utcOffset) logging.info("Successfully logged in %s (%d: %s from %s)", self.screenName, self.twitterId, self.realName, self.location)
def getAccessToken(twitter_config, allowInit=True, urlToken=None): """ This static method is used to wrap the operations of authenticating with twitter. In addition to prevent requerying twitter many times for the authentication token, this is stored in the mem-cache (TODO: investigate security concerns) to optimize performance. """ # initialise variables fnresult = None logging.debug( "Application requested Twitter Access Token: allowInit = %s, url auth token = %s", allowInit, urlToken) # if the oauth token is set, then we should regenerate the authentication token if urlToken is not None: # create the authenticator authenticator = TwitterAuth(twitter_config, urlToken) # build the access token fnresult = authenticator.buildAccessToken(URL_ACCESS_TOKEN) return fnresult # if the access key is still not known, then see if we can obtain it from the database if fnresult is None: fnresult = OAuthAccessKey.findByRequestKey(urlToken) # if we have a value, then return that value if fnresult is not None: logging.debug( "oauth key for request key '%s' retrieved from the cache or db", urlToken) return fnresult # seeing as we haven't used the cache, let's get stuck into this (If we are permitted) if allowInit: # TODO: make this work for more than just a default service user authenticator = TwitterAuth(twitter_config) else: logging.error("Unable to contact twitter, access token unknown") # return the access token return fnresult
def getAccessToken(twitter_config, allowInit = True, urlToken = None): """ This static method is used to wrap the operations of authenticating with twitter. In addition to prevent requerying twitter many times for the authentication token, this is stored in the mem-cache (TODO: investigate security concerns) to optimize performance. """ # initialise variables fnresult = None logging.debug("Application requested Twitter Access Token: allowInit = %s, url auth token = %s", allowInit, urlToken) # if the oauth token is set, then we should regenerate the authentication token if urlToken is not None: # create the authenticator authenticator = TwitterAuth(twitter_config, urlToken) # build the access token fnresult = authenticator.buildAccessToken(URL_ACCESS_TOKEN) return fnresult # if the access key is still not known, then see if we can obtain it from the database if fnresult is None: fnresult = OAuthAccessKey.findByRequestKey(urlToken) # if we have a value, then return that value if fnresult is not None: logging.debug("oauth key for request key '%s' retrieved from the cache or db", urlToken) return fnresult # seeing as we haven't used the cache, let's get stuck into this (If we are permitted) if allowInit: # TODO: make this work for more than just a default service user authenticator = TwitterAuth(twitter_config) else: logging.error("Unable to contact twitter, access token unknown") # return the access token return fnresult