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 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 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)