def _initRequestToken(self, requestTokenUrl): """ This method is used to initialise the request token so we can progress obtaining an access token to access twitter. """ # create and sign the oauth request oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_url=URL_REQUEST_TOKEN) oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None) logging.debug("created auth request: %s", oauth_request) # send the request request_result = urlfetch.fetch(url = requestTokenUrl, headers = oauth_request.to_header()) # if the response was successful, then process the response fnresult = None if request_result.status_code == 200: fnresult = oauth.OAuthToken.from_string(request_result.content) # look for the twawl user in the database request = OAuthAccessKey.findOrCreate(fnresult.key, partnerId = 'twitter') # update the user request key details request.requestKeyEncoded = fnresult.to_string() # save the user to the database request.put() else: logging.warning("Unable to obtain request token, response was %s", request_result.content) logging.debug("received request token: %s", fnresult) return fnresult
def buildAccessToken(self, accessTokenUrl): """ This static method is used to contact twitter and build the access token that will permit us to access the twitter information stream """ # if the request token has not been set, then don't attempt to get an access token if self.requestToken is None: logging.warning( "Unable to contact twitter, no request token available.") return None # initialise the oauth request logging.debug( "Attempting to build access token from request token '%s'", self.requestToken) oauth_request = oauth.OAuthRequest.from_consumer_and_token( self.consumer, token=self.accessToken, http_url=URL_ACCESS_TOKEN) oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, self.accessToken) # send the request request_result = urlfetch.fetch(url=accessTokenUrl, headers=oauth_request.to_header()) # if the response was successful, then process the response fnresult = None if request_result.status_code == 200: fnresult = oauth.OAuthToken.from_string(request_result.content) # update the user to store the access token request = OAuthAccessKey.findOrCreate(self.requestToken, partnerId='twitter') # update the user accesskey request.accessKeyEncoded = fnresult.to_string() request.put() else: logging.warning("Unable to obtain access token: %s", request_result.content) logging.debug("received access token: %s", fnresult) if fnresult: return fnresult.to_string()
def _initRequestToken(self, requestTokenUrl): """ This method is used to initialise the request token so we can progress obtaining an access token to access twitter. """ # create and sign the oauth request oauth_request = oauth.OAuthRequest.from_consumer_and_token( self.consumer, http_url=URL_REQUEST_TOKEN) oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None) logging.debug("created auth request: %s", oauth_request) # send the request request_result = urlfetch.fetch(url=requestTokenUrl, headers=oauth_request.to_header()) # if the response was successful, then process the response fnresult = None if request_result.status_code == 200: fnresult = oauth.OAuthToken.from_string(request_result.content) # look for the twawl user in the database request = OAuthAccessKey.findOrCreate(fnresult.key, partnerId='twitter') # update the user request key details request.requestKeyEncoded = fnresult.to_string() # save the user to the database request.put() else: logging.warning("Unable to obtain request token, response was %s", request_result.content) logging.debug("received request token: %s", fnresult) return fnresult
def buildAccessToken(self, accessTokenUrl): """ This static method is used to contact twitter and build the access token that will permit us to access the twitter information stream """ # if the request token has not been set, then don't attempt to get an access token if self.requestToken is None: logging.warning("Unable to contact twitter, no request token available.") return None # initialise the oauth request logging.debug("Attempting to build access token from request token '%s'", self.requestToken) oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.accessToken, http_url=URL_ACCESS_TOKEN) oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, self.accessToken) # send the request request_result = urlfetch.fetch(url = accessTokenUrl, headers = oauth_request.to_header()) # if the response was successful, then process the response fnresult = None if request_result.status_code == 200: fnresult = oauth.OAuthToken.from_string(request_result.content) # update the user to store the access token request = OAuthAccessKey.findOrCreate(self.requestToken, partnerId = 'twitter') # update the user accesskey request.accessKeyEncoded = fnresult.to_string() request.put() else: logging.warning("Unable to obtain access token: %s", request_result.content) logging.debug("received access token: %s", fnresult) if fnresult: return fnresult.to_string()