def verify(user_name,server_user_name,verify,server_user_pix='timger',server_name='sina',callback='/login',consumer_key=_conf.consumer_key,consumer_secret=_conf.consumer_secret,): import sys sys.path.append(os.path.join(DIR_PATH,'../')) from db import * res = {} #sdb = session.query(TWDBS).filter_by(TWDBS.consumer_key==consumer_key,TWDBS.consumer_secret==consumer_secret,TWDBS.user_name==user_name,TWDBS.server_user_name==server_user_name,TWDBS.server_name==server).all()[-1] sdb = session.query(TWDBS).filter(and_(TWDBS.consumer_key==consumer_key,TWDBS.consumer_secret==consumer_secret,TWDBS.user_name==user_name,TWDBS.server_user_name==server_user_name,TWDBS.server_name==server_name)).all() print sdb sdb=sdb[-1] o = OAuthHandler(consumer_key=consumer_key, consumer_secret=consumer_secret,callback=callback) request_token_sercet,request_token_key = [ i.split('=')[1] for i in sdb.request_token.split('&')] #o.request_token = oauth.OAuthToken(request_token_key,request_token_sercet) print sdb.request_token print request_token_key,'\n',request_token_sercet o.set_request_token(request_token_key,request_token_sercet) #try: access_token = o.get_access_token(verify) sdb.access_token_key = access_token.key sdb.access_token_secret = access_token.secret sdb.access_token = access_token.to_string() # res['status'] = True #except: # res['status'] = False session.flush() session.commit() session.close() return res
def get_token() : #my weibo account os.environ['REMOTE_ADDR'] = '103.8.222.56' #get code auth = OAuthHandler(consumer_key,consumer_secret,consumer_url) params = urllib.urlencode({'action':'submit','withOfficalFlag':'0','ticket':'','isLoginSina':'', \ 'response_type':'code', \ 'regCallback':'', \ 'redirect_uri':consumer_url, \ 'client_id':consumer_key, \ 'state':'', \ 'from':'', \ 'userId':username, \ 'passwd':password, \ }) authPage = urllib2.urlopen(auth.get_authorization_url()) login_url = 'https://api.weibo.com/oauth2/authorize' request = urllib2.Request(login_url, params) request.add_header("Referer", auth.get_authorization_url()) codePage = urllib2.urlopen(request) code = codePage.geturl().split('code%3D')[1] #use code exchange token tokenData = 'client_id='+consumer_key+'&client_secret='+consumer_secret+'&grant_type=authorization_code&redirect_uri='+consumer_url+'&code='+code request = urllib2.Request("https://api.weibo.com/oauth2/access_token", tokenData) tokenPage = urllib2.urlopen(request) tokenJson = json.loads(tokenPage.read()) token = tokenJson['access_token'] db.update('token',where="id =$id", vars={'id':'weibo'}, token = token)
def get_weibo_user(): auth = OAuthHandler(consumer_key, consumer_secret) # Get currrent user access token from session access_token = session['oauth_access_token'] auth.setToken(access_token.key, access_token.secret) api = API(auth) # Get info from weibo return api.me()
def post(self, site): if site == "wb": content = self.get_argument("content") access_token = self.session.get("oauth_access_token") auth = OAuthHandler(options.SINA_APP_KEY, options.SINA_APP_SECRET) auth.set_access_token(access_token.key, access_token.secret) api = API(auth) api.update_status(content) self.finish(json.dumps("success"))
def login(): session['login_ok_url'] = get_referer() callback = 'http://appstack.sinaapp.com/login_callback' auth = OAuthHandler(consumer_key, consumer_secret, callback) # Get request token and login url from the provider url = auth.get_authorization_url() session['oauth_request_token'] = auth.request_token # Redirect user to login return redirect(url)
def get_api(): import sys, os.path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from weibopy import OAuthHandler, API, WeibopError c = get_conf() try: o = OAuthHandler(c['consumer_key'], c['consumer_secret']) o.setToken(c['access_token_key'], c['access_token_secret']) except KeyError, e: sys.stderr.write("you should run get_oauthed.py first.\n")
def login_callback(): # This is called by the provider when user has granted permission to your app verifier = request.args.get('oauth_verifier', None) auth = OAuthHandler(consumer_key, consumer_secret) request_token = session['oauth_request_token'] del session['oauth_request_token'] # Show the provider it's us really auth.set_request_token(request_token.key, request_token.secret) # Ask for a temporary access token session['oauth_access_token'] = auth.get_access_token(verifier) return redirect(session.get('login_ok_url', '/'))
def login_with_sinaid(request): callback = request.build_absolute_uri('/check_user_login_sina') auth = OAuthHandler(consumer_key=consumer_key, consumer_secret=consumer_secret , callback=callback) try: authorization_url = auth.get_authorization_url() except WeibopError: request.session['errormessage'] = ugettext('Connect sina twitter fail, please retry') return login(request) request_token = auth.request_token request.session['oauth_request_token'] = request_token return HttpResponseRedirect(authorization_url)
def get_api(consumer_key,consumer_sercet,access_token_key,access_token_sercet,access_token,user_name,server_user_name,server='sina'): import sys, os.path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) sys.path.append(os.path.join(DIR_PATH,'../')) from weibopy import OAuthHandler, API, WeibopError from db import * sdb = session.query(TWDBS).filter(TWDBS.consumer_key==consumer_key,TWDBS.consumer_secret==consumer_secret,TWDBS.user_name==user_name,TWDBS.server_user_name==server_user_name,TWDBS.server==server).all()[-1] try: o = OAuthHandler(consumer_key, consumer_secret) o.setToken(sdb.access_token_key,sdb.access_token_secret) except KeyError, e: sys.stderr.write("you should run get_oauthed.py first.\n")
def login_with_sinaid(request): callback = request.build_absolute_uri('/check_user_login_sina') auth = OAuthHandler(consumer_key=consumer_key, consumer_secret=consumer_secret, callback=callback) try: authorization_url = auth.get_authorization_url() except WeibopError: request.session['errormessage'] = ugettext( 'Connect sina twitter fail, please retry') return login(request) request_token = auth.request_token request.session['oauth_request_token'] = request_token return HttpResponseRedirect(authorization_url)
def __init__(self, *argc, **argkw): super(BaseHandler, self).__init__(*argc, **argkw) self.path = '' self.session = session.TornadoSession(self.application.session_manager, self) if self.session.get('platform') == 'weibo': self.sina_access_token = self.session.get('oauth_access_token') auth = OAuthHandler(options.SINA_APP_KEY, options.SINA_APP_SECRET) auth.set_access_token(self.sina_access_token.key, self.sina_access_token.secret) self.sina_api = API(auth) elif self.session.get('platform') == 'renren': self._userid = int(self.get_user_id()) print "renren ok" elif self.session.get('platform') == 'douban': self._userid = int(self.get_user_id()) print "douban ok"
def check_user_login_sina(request): try: oauth_verifier = request.GET.get('oauth_verifier', None) request_token = request.session.get('oauth_request_token', None) request.session['oauth_request_token'] = None auth = OAuthHandler(consumer_key, consumer_secret) auth.request_token = request_token access_token = auth.get_access_token(oauth_verifier) request.session['oauth_access_token'] = access_token api = API(auth) sina_user = api.me() user_in_session = request.session.get('user', None) isnewuser = False if user_in_session == None: user, isnewuser = UserService().getorCreateCoolUserBySinaUser( sina_user) user = UserService().getUser(user) else: errormessage = UserService().updateSinaID(user_in_session, sina_user) if errormessage: request.session['errormessage'] = errormessage user = UserService().getUser(user_in_session) request.session['user'] = user newmessagecount = UserService().getMessageCount({'isread': False}, user) user.newmessagecount = newmessagecount UserService().updateCoolUser(user) if isnewuser: back_to_url = '/setting' else: back_to_url = request.session.get('login_back_to_url', '/') return HttpResponseRedirect(back_to_url) except WeibopError: errormessage = ugettext( 'Auth sina twitter account failed, please retry.') request.session['errormessage'] = errormessage return login(request)
def main(): c = get_conf() o = OAuthHandler(c['consumer_key'], c['consumer_secret']) c['authorization_url'] = o.get_authorization_url() c['request_token'] = o.request_token print c['authorization_url'] verifier = raw_input('click the url above, then input the verifier: ') verifier = verifier.strip() c['verifier'] = verifier try: access_token = o.get_access_token(verifier) c['access_token_key'] = access_token.key c['access_token_secret'] = access_token.secret c['access_token'] = access_token.to_string() set_conf(c) print 'oauth succeeded' except: print 'oauth failed'
def get_authorization_url(user_name,server_user_name,server_user_pix='timger',server='sina',callback=None,consumer_key=_conf.consumer_key,consumer_secret=_conf.consumer_secret,): import sys sys.path.append(os.path.join(DIR_PATH,'../')) from db import * o = OAuthHandler(consumer_key, consumer_secret,callback=callback) authorization_url = o.get_authorization_url() request_token = o.request_token print request_token sdb = TWDBS(consumer_key=consumer_key,consumer_secret=consumer_secret,request_token=str(request_token)) session.add(sdb) session.commit() session.close() res = {} res['request_token'] = request_token res['authorization_url'] = authorization_url print authorization_url return res
def check_user_login_sina(request): try: oauth_verifier = request.GET.get('oauth_verifier', None) request_token = request.session.get('oauth_request_token', None) request.session['oauth_request_token'] = None auth=OAuthHandler(consumer_key, consumer_secret) auth.request_token=request_token access_token=auth.get_access_token(oauth_verifier) request.session['oauth_access_token'] = access_token api = API(auth) sina_user = api.me() user_in_session = request.session.get('user', None) isnewuser = False if user_in_session == None: user, isnewuser = UserService().getorCreateCoolUserBySinaUser(sina_user) user = UserService().getUser(user) else: errormessage = UserService().updateSinaID(user_in_session, sina_user) if errormessage: request.session['errormessage'] = errormessage user = UserService().getUser(user_in_session) request.session['user'] = user newmessagecount = UserService().getMessageCount({'isread':False}, user) user.newmessagecount = newmessagecount UserService().updateCoolUser(user) if isnewuser: back_to_url = '/setting' else: back_to_url = request.session.get('login_back_to_url', '/') return HttpResponseRedirect(back_to_url) except WeibopError: errormessage = ugettext('Auth sina twitter account failed, please retry.') request.session['errormessage'] = errormessage return login(request)
#!/usr/bin/python #-*- coding: utf-8 -*- #------------------------------------------------------- #Var #------------------------------------------------------- ###AppInfo APP_KEY = "3786122414" APP_SECRET = "3f0d9e2f3ae7f117755971dc358f7b5b" #------------------------------------------------------- from weibopy import OAuthHandler, oauth, WeibopError ###获得应用的token auth = OAuthHandler(APP_KEY, APP_SECRET) ###获得用户OAuth登录的url auth_url = auth.get_authorization_url() print "Please Auth: "+auth_url verifier = raw_input("PIN: ").strip() access_token = auth.get_access_token(verifier) ###获得access_token ###发布微博 api = API(auth) status = api.update_status(status="Hello World") ###获得access_token后,以后便可以通过 ###auth.set_access_token(access_token.key, access_token.secret) ###完成授权.