Exemplo n.º 1
0
    def get(self, *args, **kwargs):
        code = self.get_argument('code')
        code = code.strip()

        # print code
        # client = Client(options.QQ_KEY, options.QQ_SECRET,
        #         site='https://graph.qq.com',
        #         authorize_url='https://graph.qq.com/oauth2.0/authorize',
        #         token_url='https://graph.qq.com/oauth2.0/token')
        # access_token = client.auth_code.get_token(code, redirect_uri=options.QQ_CALLBACK, parse='query')
        #
        #
        # # for key in access_token.params.keys():
        # #     print access_token.params.get(key),'0000000000000'
        #
        #
        #
        # token = access_token.token
        # client_id = access_token.params.get('client_id')
        author_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code'

        content = urllib.urlopen(
            'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code'
            % (options.WEIXIN_KEY, options.WEIXIN_SECRET, code)).read()
        token_info = ujson.loads(content)
        openid = token_info.get('openid')
        access_token = token_info.get('access_token')
        user_info = urllib.urlopen(
            'https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s'
            % (access_token, openid)).read()
        user = ujson.loads(user_info)

        user_service = UserServices(self.db)
        exist_user = user_service.get_user_by_weixin(openid)
        print 'openid:', openid
        new_user = user_service.create_user_by_weixin(
            user=exist_user,
            nick=user.get('nickname'),
            photo=user.get('headimgurl'),
            weixin=openid,
            sex=user.get('sex'))
        print new_user.id
        cookies = user_service.user_format(new_user)
        self.set_secure_cookie('loginuser', ujson.dumps(cookies))
        self.redirect('/')
Exemplo n.º 2
0
 def post(self):
     #login_count = self.mcache.get(self.get_client_ip+'_login_count')
     next = self.get_argument('next', '/topic')
     back_url = urlparse.urljoin(self.request.full_url(),
                                 self.get_argument('next', '/topic'))
     data = {}
     username = self.get_argument('username', '')  # 用户名
     passwd = self.get_argument('passwd', '')  # 密码
     if not username or not passwd:
         data['error'] = '账号和密码不能为空'
         self.echo('view/login/login.html', {
             'next': next,
             'data': data
         },
                   layout='')
     else:
         user_service = UserServices(self.db)
         status, user = user_service.check_user_login_pwd(username, passwd)
         if status:
             user.Flast_visit = datetime.datetime.now()
             user.Flast_visit_ip = self.get_client_ip
             user.Fvisit_times = user.Fvisit_times + 1
             cookies = user_service.user_format(user)
             self.set_secure_cookie(
                 'loginuser',
                 ujson.dumps(cookies),
                 expires_days=100 if int(
                     self.get_argument('login_forever', 0)) else 0.1,
                 httponly=True)
             self.db.add(user)
             self.db.commit()
             self.redirect(back_url)
         else:
             data['error'] = '账号或密码错误'
             self.echo('view/login/login.html', {
                 'next': next,
                 'data': data
             },
                       layout='')
Exemplo n.º 3
0
    def get(self, *args, **kwargs):
        code = self.get_argument('code')
        code = code.strip()
        client = Client(
            options.QQ_KEY,
            options.QQ_SECRET,
            site='https://graph.qq.com',
            authorize_url='https://graph.qq.com/oauth2.0/authorize',
            token_url='https://graph.qq.com/oauth2.0/token')
        access_token = client.auth_code.get_token(
            code, redirect_uri=options.QQ_CALLBACK, parse='query')

        # for key in access_token.params.keys():
        #     print access_token.params.get(key),'0000000000000'

        token = access_token.token
        client_id = access_token.params.get('client_id')
        content = urllib.urlopen(
            'https://graph.qq.com/oauth2.0/me?access_token=' +
            access_token.token).read()
        open_id = ujson.loads(content[content.find('{'):content.find('}') +
                                      1]).get('openid')
        user_info = get_oauth_user_info(
            'https://graph.qq.com/user/get_user_info',
            oauth_consumer_key=options.QQ_KEY,
            access_token=access_token.token,
            format='json',
            openid=open_id)
        user = ujson.loads(user_info)
        #['figureurl_2','nickname','open_id']

        # print user.get('figureurl_2')
        # print open_id
        # print user.get('nickname')
        # print user.get('gender')
        #
        # print user_info

        user_service = UserServices(self.db)
        exist_user = user_service.get_user_by_qq(open_id)
        #user = user_service.get_user_by_weibo(user_json.get('idstr'))

        user = user_service.create_user_by_qq(user=exist_user,
                                              nick=user.get('nickname'),
                                              photo=user.get('figureurl_qq_2'),
                                              qq=open_id)
        #
        # uid = self.user_uid(user_name=kargs.get('open_id',''))
        # user.uid = uid
        # user.nick = kargs.get('nick','')
        # user.email = kargs.get('email','')
        # user.phone = kargs.get('phone','')
        # user.photo = kargs.get('photo', '')
        # user.is_employee = kargs.get('is_employee',0)
        # user.sex = kargs.get('sex','')
        # user.last_visit = datetime.now()
        # user.last_visit_ip = kargs.get('last_visit_ip','')
        # user.visit_times = kargs.get('visit_times',0)
        # user.regist_from = kargs.get('regist_from','qq')
        # user.find_pw_url = kargs.get('find_pw_url','')
        # user.status = kargs.get('status','normal')
        # user.avatar = kargs.get('avatar','')
        # user.sign_text = kargs.get('sign_text','')

        # qq_weibo_info = get_oauth_user_info('https://graph.qq.com/user/get_info',oauth_consumer_key=options.QQ_KEY,access_token=access_token.token,format='json',openid=open_id)
        # print qq_weibo_info
        # print 'save user'
        print user.id
        cookies = user_service.user_format(user)
        self.set_secure_cookie('loginuser', ujson.dumps(cookies))
        self.redirect('/')