def GET(self): client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, callback=CALLBACK_URL) request_token = client.get_request_token() # 保存request_token: self.save_request_token(request_token.oauth_token, request_token.oauth_token_secret) url = client.get_authorize_url(request_token.oauth_token) # redirect to url raise web.found(url)
def sinaWeiBo(appKey,appSecret,infoDb,user,content): access_token=infoDb.get(user,None) if not access_token: client = APIClient(app_key=appKey, app_secret=appSecret) # 用appKey创建一个client request_token = client.get_request_token() # 1.app认证,如果认证成功,有sina返回一个request_token url="http://api.t.sina.com.cn/oauth/authorize?oauth_token=%s"%(request_token.oauth_token) # 需要用户对该app进行授权,这样保证app不会获取用户的帐号和密码 webbrowser.open(url) oauth_verifier = raw_input('PIN: ').strip() # 验证用户是否对app授权 即2.授权认证 request_token = OAuthToken(request_token.oauth_token, request_token.oauth_token_secret, oauth_verifier) # 构造新一个request_token client = APIClient(app_key=appKey, app_secret=appSecret, token=request_token) access_token = client.get_access_token() # 向sina获取access_token,即3.代理访问认证 infoDb.setdefault(user,access_token) client = APIClient(app_key=appKey, app_secret=appSecret, token=access_token) client.post.statuses__update(status=content.decode('utf-8').encode('utf-8')) for object in client.get.statuses__user_timeline(): # 获取该user发布的前20条微博 print "" # id = object["id"] text = object["text"] created_at = object["created_at"] print str(created_at)+":"+ text.encode('gb18030'), if 'retweeted_status' in object: object=object['retweeted_status'] print "retweeted:",object['text'].encode('gb18030'),
class cronjob(): def POST(self): for i in db.select('account_info'): self.client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, token=OAuthToken(oauth_token=i.oauth_token, oauth_token_secret=i.oauth_token_secret)) if self.process_new_rts(i.rt_since_id): db.update('account_info', where = 'id = %s' % i.id, rt_since_id = self.since_id) # 处理新转发(retweet)的函数 def process_new_rts(self, rt_since_id): rts = self.get_new_rts(rt_since_id) if not rts: return False else: for rt in reversed(rts): #print '~~~~%s~~~~' % i if self.forbidden_tweet(rt): #不符合转发条件的 continue try: self.parse_rt(rt) return True # 每次循环只转发一次就好了 except urllib2.HTTPError: continue return False def get_new_rts(self, rt_since_id): rts = self.client.statuses__mentions(since_id=rt_since_id, count=20) return rts def parse_rt(self, rt): self.client.post.statuses__repost(id=rt.id, status='//@%s: %s' % (rt.user.screen_name, rt.text.replace(u'''//@广西北海中学:''', ''))) self.since_id = rt.id def forbidden_tweet(self, rt): def is_too_many_retweets(rt): try: tid = str(rt.retweeted_status.id) except KeyError: logging.debug('-'*80 + 'KeyError' + rt.text) tid = str(rt.id) #print '----%s----%s----%s' % (rt, rt.retweeted_status.id, (tid)) times = cache.get(tid) if times: logging.info("%s: %s times %s" % (tid, times, rt.retweeted_status.text)) if times > 20: return True else: cache.incr(tid) else: cache.set(tid, '1') return False def no_comment(rt): if rt.text.startswith('//'): return True if rt.user.id == 1468317623 or is_too_many_retweets(rt) or no_comment(rt): #不符合转发条件的 return True else: return False
def get_user_info_v1(uid, oauth_token, oauth_token_secret): weiboClientV1 = APIClient(app_key, app_secret, OAuthToken(oauth_token, oauth_token_secret)) user = None try: user = weiboClientV1.post.users__show(source=app_key, user_id=uid) except Exception, e: print e
def sinaWeiBo2(appKey,appSecret,name): client = APIClient(app_key=appKey, app_secret=appSecret,isAuthorization=False) for object in client.get.statuses__user_timeline(source=appKey, screen_name=name): #获取该user发布的前20条微博 print "" #id = object["id"] text = object["text"] created_at = object["created_at"] print str(created_at)+":"+ text.encode('gb18030'), if 'retweeted_status' in object: object=object['retweeted_status'] print "retweeted:",object['text'].encode('gb18030'),
def GET(self): # 获取URL参数: oauth_token = web.input().get('oauth_token') oauth_verifier = web.input().get('oauth_verifier') # 使用oauth_token查表获取上一步保存的oauth_token_secret: oauth_token_secret = cache.get(oauth_token.encode('ascii')) # 构造完整的request token: request_token = OAuthToken(oauth_token, oauth_token_secret, oauth_verifier) # 用request token获取access token: client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, token=request_token) access_token = client.get_access_token() # 通过access token调用一个API获取用户uid信息: client = APIClient(APP_KEY, APP_SECRET, access_token) account = client.account__verify_credentials() uid = str(account.id) # 保存uid和access token以后使用: self.save_access_token_to_db(uid, access_token.oauth_token, access_token.oauth_token_secret) try: # 发表一条授权成功的微博 client.post.statuses__update(status="本微博帐号已成为一个基于OPENTree0应用的树洞,欢迎大家来此吐槽生活。树洞的使用方法请见OPENTree0主页:http://opentree0.sinaapp.com/") except HTTPError: logging.error("重复发送微博。") raise web.found('/success')
def GetBlogClient(uname, passw): APP_KEY = u'XXXXXXXXXX' # app key APP_SECRET = u'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # app secret #实例化APIClient client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET) #获取OAuth request token reqToken = client.get_request_token() #用户授权url auth_url = client.get_authorize_url(reqToken) post_data = urllib.urlencode({ "action": "submit", "forcelogin": "", "from": "", "oauth_callback": "http://api.weibo.com/oauth2/default.html", "oauth_token": reqToken.oauth_token, "passwd": passw, "regCallback": "", "ssoDoor": "", "userId": uname, "vdCheckflag": 1, "vsnval": "" }) mat = re.search( r'&oauth_verifier=(.+)', urllib2.urlopen( urllib2.Request("http://api.t.sina.com.cn/oauth/authorize", post_data, headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1)', 'Referer': auth_url })).url) if mat: client = APIClient( APP_KEY, APP_SECRET, OAuthToken(reqToken.oauth_token, reqToken.oauth_token_secret, mat.group(1))) #返回APIClient return APIClient(APP_KEY, APP_SECRET, client.get_access_token()) else: raise Exception()
def GetBlogClient(uname, passw): client = APIClient(app_key=app_key, app_secret=app_secret) reqToken = client.get_request_token() auth_url = client.get_authorize_url(reqToken) return APIClient(app_key, app_secret, client.get_access_token())
def POST(self): for i in db.select('account_info'): self.client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, token=OAuthToken(oauth_token=i.oauth_token, oauth_token_secret=i.oauth_token_secret)) if self.process_new_rts(i.rt_since_id): db.update('account_info', where = 'id = %s' % i.id, rt_since_id = self.since_id)