def get_access_token(self, request_token): url = API_HOME + '/oauth/access_token' client = OAuthClient(self.consumer, token=request_token) resp, content = client.request(url) if resp['status'] != '200': raise Exception("Invalid response %s." % resp['status']) return OAuthToken.from_string(content)
def fetch_twits(self): logging.debug("user(%s)::fetch_twits", self.user_id) client = OAuthClient('twitter',self) if self.access_token: logging.debug ("TUser::access_token = %s", self.access_token) client.token = self.access_token the_twits = client.get('/statuses/friends_timeline','GET', (200,),count=200) url_regex = re.compile(r'''((?:mailto:|ftp://|http://)[^ <>'"{}|\\^`[\]]*)''') for twit in the_twits: if (re.search("http", twit['text'])): twit_id = twit['id'] twit_text = twit['text'] twit_url = TUrl.extract_url_from_text(twit['text']) screen_name = twit['screen_name'] public_name = twit['public_name'] profile_image_url = twit['profile_image_url'] logging.debug(" --> twitter_id = %s", twit_id) logging.debug(" --> twitter_text = %s", twit_text) logging.debug(" --> twit_url = %s", twit_url) logging.debug(" --> screen_name = %s", screen_name) logging.debug(" --> public_name = %s", public_name) logging.debug(" --> profile_image_url = %s", profile_image_url) #turl = TUrl.get_or_insert(twit_id, # text = twit_text, # url = twit_url, # screen_name = twit['screen_name'], else: logging.debug("TUser: access_token is nil!")
def __init__(self, oauth_params, user_agent=None, desktop=False, force_login=False, proxy=None, version=1): """ Instantiates an instance of mtweets. Takes optional parameters for authentication and such (see below). Parameters: oauth_params - key, secret tokens for OAuth desktop - define if the API will be used for desktop apps or web apps. force_login - Optional. Forces the user to enter their credentials to ensure the correct users account is authorized. user_agent - User agent header. proxy - An object detailing information, in case proxy user/authentication is required. Object passed should be something like... proxyobj = { "username": "******", "password": "******", "host": "http://fjanfjasnfjjfnajsdfasd.com", "port": 87 } version (number) - Twitter supports a "versioned" API as of Oct. 16th, 2009 - this defaults to 1, but can be overridden on a class and function-based basis. ** Note: versioning is not currently used by search.twitter functions; when Twitter moves their junk, it'll be supported. """ # setting super class variables OAuthClient.__init__(self, OAuthConsumer(*oauth_params), None) # setting the the needed urls self._url_request = "https://api.twitter.com/oauth/request_token" self._url_access = "https://api.twitter.com/oauth/access_token" self._url_autorize = "https://api.twitter.com/oauth/authorize" self._url_authenticate = "https://api.twitter.com/oauth/authenticate" self._signature_method = OAuthSignatureMethod_HMAC_SHA1() # subclass variables self.apiVersion = version self.proxy = proxy self.user_agent = user_agent self.desktop = desktop self.force_login = force_login if self.proxy is not None: self.proxyobj = urllib2.ProxyHandler({'http': 'http://%s:%s@%s:%d'%(self.proxy["username"], self.proxy["password"], self.proxy["host"], self.proxy["port"])}) self.opener = urllib2.build_opener(self.proxyobj) else: self.opener = urllib2.build_opener() if self.user_agent is not None: self.opener.addheaders = [('User-agent', self.user_agent)]
def get(self): upload_url = blobstore.create_upload_url('/upload') files = db.GqlQuery("SELECT * " "FROM UpFile " "ORDER BY uploaded DESC") template_values = { 'files': files, 'upload_url': upload_url, 'url_linktext': "asdfsdf", "nickname": False } user = users.get_current_user() client = OAuthClient('twitter', self) gdata = OAuthClient( 'google', self, scope='https://www.googleapis.com/auth/userinfo.profile') if client.get_cookie(): info = client.get('/account/verify_credentials') template_values['nickname'] = info['screen_name'] template_values['service'] = 'twitter' template = jinja_environment.get_template('index.html') self.response.out.write(template.render(template_values))
def get_request_token(self, callback=None): url = API_HOME + '/oauth/request_token' client = OAuthClient(self.consumer) resp, content = client.request(url, callback=callback, force_auth_header=False) if resp['status'] != '200': raise Exception("Invalid response %s." % resp['status']) return OAuthToken.from_string(content)
def execute(method="GET", parameters=None): url = API_HOME for arg in args: url += '/%s' % arg url += '.json' client = OAuthClient(self.consumer, self.access_token) resp, content = client.request(url, method=method, parameters=parameters) if resp['status'] != '200': raise Exception("Invalid response %s." % resp['status']) return json.loads(content)
def get(self): upload_url = blobstore.create_upload_url("/upload") files = db.GqlQuery("SELECT * " "FROM UpFile " "ORDER BY uploaded DESC") template_values = {"files": files, "upload_url": upload_url, "url_linktext": "asdfsdf", "nickname": False} user = users.get_current_user() client = OAuthClient("twitter", self) gdata = OAuthClient("google", self, scope="https://www.googleapis.com/auth/userinfo.profile") if client.get_cookie(): info = client.get("/account/verify_credentials") template_values["nickname"] = info["screen_name"] template_values["service"] = "twitter" template = jinja_environment.get_template("index.html") self.response.out.write(template.render(template_values))
def get(self): user = users.get_current_user() tuser = TUser.get_or_insert(user.user_id(), user_id=user.user_id()) client = OAuthClient('twitter',self) if tuser.access_token is None: if client.get_cookie(): logging.debug("registered user finished oauth procedure") credentials = client.get('/account/verify_credentials') tuser.access_token = OAuthAccessToken.get_by_key_name(client.get_cookie()) tuser.screen_name = credentials['screen_name'] tuser.profile_image_url = credentials['profile_image_url'] tuser.name = credentials['name'] tuser.put() view = 'just_logged.html' template_values = { 'username':user.nickname(), 'email':user.email() } else: view = 'not_logged.html' template_values = { 'username': user.nickname(), 'email': user.email() } else: template_values = { 'username': user.nickname(), 'email': user.email() } view = 'index.html' path = os.path.join(os.path.dirname(__file__) + '/templates/' + view ) self.response.out.write(template.render(path, template_values))
class UserMgr(): def __init__(self, mainDir): self.__userStore = UserStore(mainDir) self.__urlResolverMgr = UrlSyncResolverManager(os.path.join(mainDir, "urlResolverCache.db2")) self.__urlResolverMgr.start() def doJob(self, token, userId, stoppable, screenName=None): return UserFeatures(self.__userStore, self.__urlResolverMgr, userId, screenName, token, stoppable) def close(self): self.__urlResolverMgr.stop() self.__userStore.close() class Token(): def __init__(self, tup): self.key = tup[0] self.secret = tup[1] if __name__ == "__main__": mainDir="/media/eea1ee1d-e5c4-4534-9e0b-24308315e271/pynews/user" mgr = UserMgr(mainDir) auth = OAuthClient(os.environ["consumer_key"], os.environ["consumer_secret"]) token = Token(auth.accessToken()) for userId in ["4iHD", "soldierkam"]: mgr.doJob(token, userId) mgr.close()
def doProxy(self, method): origUrl = self.request.url origBody = self.request.body (scm, netloc, path, params, query, _) = urlparse(origUrl) if path == '/': self.myOutput('text/html', 'here is the proxy of \"twitter.com\" by GTAP %s !' % (gtapVersion)) else: #logging.debug(self.request.headers) auth_header = None if 'Authorization' in self.request.headers : auth_header = self.request.headers['Authorization'] elif 'X-Authorization' in self.request.headers : auth_header = self.request.headers['X-Authorization'] headers = {} use_oauth = False if auth_header != None : #auth_header = self.request.headers['Authorization'] auth_parts = auth_header.split(' ') if auth_parts[0].lower() == 'basic': user_pass_parts = base64.b64decode(auth_parts[1]).split(':') oauth_name = user_pass_parts[0] oauth_key = user_pass_parts[1] use_oauth = True path_parts = path.split('/') path_parts = path_parts[2:] path_parts.insert(0,'') #logging.debug(path_parts) path = '/'.join(path_parts).replace('//','/') if path_parts[1] == 'search': netloc = 'search.twitter.com' newpath = path[7:] elif path_parts[1] == 'api' and path_parts[2] == '1': netloc = 'api.twitter.com' newpath = path[4:] elif path_parts[1] == 'api': netloc = 'twitter.com' newpath = path[4:] elif path_parts[1] == '1': netloc = 'api.twitter.com' newpath = path else: netloc = 'twitter.com' newpath = path if newpath == '/' or newpath == '': self.myOutput('text/html', 'here is the proxy of \"'+ netloc + '\" by GTAP %s !' % (gtapVersion)) else: if use_oauth: newUrl = urlunparse(("https", netloc, newpath, params, '', '')) client = OAuthClient('twitter', self) client.key_name = oauth_key client.specifier = oauth_name #logging.info(('method',method)) if method.upper() == 'POST': #logging.info(dict([(k,v) for k,v in parse_qsl(origBody)])) data = client.post_raw(newUrl,method,**dict([(k,v) for k,v in parse_qsl(origBody)])) else: data = client.get_raw(newUrl,method,**dict([(k,v) for k,v in parse_qsl(query)])) else: newUrl = urlunparse(("https", netloc, newpath, params, query, '')) data = urlfetch.fetch(newUrl, payload=origBody, method=method, headers=headers) skipped_headers = ['status', 'via'] for k in data.headers: if is_hop_by_hop(k): continue if k.lower() in skipped_headers: continue k = k \ .replace('x-transaction', 'X-Transaction') \ .replace('x-ratelimit-limit', 'X-RateLimit-Limit') \ .replace('x-ratelimit-remaining', 'X-RateLimit-Remaining') \ .replace('x-runtime', 'X-Runtime') \ .replace('x-ratelimit-class', 'X-RateLimit-Class') \ .replace('x-revision', 'X-Revision') \ .replace('x-ratelimit-reset', 'X-RateLimit-Reset') del self.response.headers[k] self.response.headers[k] = data.headers[k]; self.response.set_status(data.status_code) #logging.debug(headers) #logging.debug(data.status_code) #logging.debug(data.headers) #logging.debug(self.response.headers) #logging.debug(data.content) self.response.out.write(data.content)