Exemplo n.º 1
0
 def get_access_token(self, authorization_code):
     qs = {
         "client_id": self.apikey,
         "client_secret": self.apikey_secret,
         "redirect_uri": self.redirect_uri,
         "grant_type": "authorization_code",
         "code": authorization_code,
     }
     qs = urllib.urlencode(qs)
     resp, content = httplib2_request(self.access_token_uri, "POST", body=qs)
     if resp.status != 200:
         return view.error404('Connection failed') #note:need to change view name "error404"
         #raise OAuthLoginError('get_access_token, status=%s:reason=%s:content=%s' \
         #        %(resp.status, resp.reason, content))
     return json_decode(content)
Exemplo n.º 2
0
    def GET(self):
        data = web.input()

        if data.has_key('code'):
            code = data.code
            provider = 'douban'
            d = config.APIKEY_DICT.get(provider)

            login_service = None
            if provider == config.OPENID_DOUBAN:
                openid_type = config.OPENID_TYPE_DICT[config.OPENID_DOUBAN]
                login_service = DoubanLogin(d['key'], d['secret'],
                                            d['redirect_uri'])
            # elif provider == config.OPENID_SINA:
            #     openid_type = config.OPENID_TYPE_DICT[config.OPENID_SINA]
            #     login_service = SinaLogin(d['key'], d['secret'], d['redirect_uri'])
            # else:
            #     ## 处理以oauth1的方式授权的
            #     if provider == config.OPENID_QQ:
            #         user = _qqweibo_callback(request)

            #     elif provider == config.OPENID_TWITTER:
            #         user = _twitter_callback(request)

            #     if user:
            #         _add_sync_task_and_push_queue(provider, user)
            #         return redirect(url_for('index'))
            #     else:
            #         return "connect to %s fail" % provider

            try:
                token_dict = login_service.get_access_token(code)
            except OAuthLoginError, e:
                return view.error404(
                    'Connection failed'
                )  #note:need to change view name "error404"

            if not (token_dict and token_dict.get("access_token")):
                return (401, "no_access_token")
            try:
                user_info = login_service.get_user_info(
                    token_dict.get("access_token"), token_dict.get("uid"))
            except OAuthLoginError, e:
                return (401, e.msg)
Exemplo n.º 3
0
    def GET(self):
        data = web.input()
        
        if data.has_key('code'):
            code = data.code
            provider = 'douban'
            d = config.APIKEY_DICT.get(provider)

            login_service = None
            if provider == config.OPENID_DOUBAN:
                openid_type = config.OPENID_TYPE_DICT[config.OPENID_DOUBAN]
                login_service = DoubanLogin(d['key'], d['secret'], d['redirect_uri'])
            # elif provider == config.OPENID_SINA:
            #     openid_type = config.OPENID_TYPE_DICT[config.OPENID_SINA]
            #     login_service = SinaLogin(d['key'], d['secret'], d['redirect_uri'])
            # else:
            #     ## 处理以oauth1的方式授权的
            #     if provider == config.OPENID_QQ:
            #         user = _qqweibo_callback(request)

            #     elif provider == config.OPENID_TWITTER:
            #         user = _twitter_callback(request)

            #     if user:
            #         _add_sync_task_and_push_queue(provider, user)
            #         return redirect(url_for('index'))
            #     else:
            #         return "connect to %s fail" % provider

            try:
                token_dict = login_service.get_access_token(code)
            except OAuthLoginError, e:
                return view.error404('Connection failed') #note:need to change view name "error404"

            if not ( token_dict and token_dict.get("access_token") ):
                return(401, "no_access_token")
            try:
                user_info = login_service.get_user_info(
                    token_dict.get("access_token"), token_dict.get("uid"))
            except OAuthLoginError, e:
                return(401, e.msg)
Exemplo n.º 4
0
                #     #如果未填写邮箱地址
                #     else:
                #         print '55555555555555'
                #         #先把user_info信息放到 session 中,供跳转后取出使用
                #         session.douban_callback(user_info)
                #         #跳转到邮箱设置页面
                #         raise web.seeother('/welcome/'+ user_info['uid'])

                # #如果用户表中没有此用户
                # else:
                #     #先把user_info信息放到 session 中,供跳转后取出使用
                #     session.douban_callback(user_info)
                #     #跳转到邮箱设置页面
                #     raise web.seeother('/welcome/'+ user_info['uid'])
            else:
                return view.error404('Connection failed')
        else:
            return view.error404('Connection failed')


#connect_douban
class connect_douban:
    def GET(self):
        provider = 'douban'
        d = config.APIKEY_DICT.get(provider)
        raise web.seeother(
            'https://www.douban.com/service/auth2/auth?client_id=' + d['key'] +
            '&redirect_uri=' + d['redirect_uri'] + '&response_type=code')


## User数据接口
Exemplo n.º 5
0
                #     #如果未填写邮箱地址
                #     else:
                #         print '55555555555555'
                #         #先把user_info信息放到 session 中,供跳转后取出使用
                #         session.douban_callback(user_info)
                #         #跳转到邮箱设置页面
                #         raise web.seeother('/welcome/'+ user_info['uid'])

                # #如果用户表中没有此用户
                # else:
                #     #先把user_info信息放到 session 中,供跳转后取出使用
                #     session.douban_callback(user_info)
                #     #跳转到邮箱设置页面
                #     raise web.seeother('/welcome/'+ user_info['uid'])
            else:
                return view.error404('Connection failed')
        else:
            return view.error404('Connection failed')

#connect_douban
class connect_douban:
    def GET(self):
        provider = 'douban'
        d = config.APIKEY_DICT.get(provider)
        raise web.seeother('https://www.douban.com/service/auth2/auth?client_id='+ d['key'] +'&redirect_uri='+ d['redirect_uri'] +'&response_type=code')

## User数据接口
class AbsUserData(object):

    def __init__(self, data):
        if data:
Exemplo n.º 6
0
    def GET(self):
        data = web.input()
        if 'code' not in data:
            return view.error404('Connection failed')

        code = data.code
        provider = 'douban'
        d = config.APIKEY_DICT.get(provider)
        if provider == config.OPENID_DOUBAN:
            openid_type = config.OPENID_TYPE_DICT[config.OPENID_DOUBAN]
            douban_login = DoubanLogin(d['key'], d['secret'], d['redirect_uri'])
        token_dict = douban_login.get_access_token(code)
        if not token_dict or not token_dict.get("access_token"):
            return(401, "no_access_token")

        user_info = douban_login.get_user_info(token_dict.get("access_token"), token_dict.get("uid"))
        if not user_info:
            return view.error404('Connection failed')

        douban_id = user_info['id']
        nickname = user_info[u'name']
        avatarPath = user_info['avatar']

        #判断邮箱激活表中是否有此豆瓣ID
        if users.douban_id_exist_in_table_confirm_email(int(douban_id)):
            #如果此用户填写过email
            if users.get_confirm_email_by_douban_id(douban_id).email:
                info = users.get_confirm_email_by_douban_id(douban_id)
                #如果填写的邮箱已经验证
                if info.confirmed:
                    #更新用户邮箱 和 昵称 等资料
                    users.update_user_by_douid(
                        douban_id,
                        nickname = nickname,
                        avatarPath = avatarPath,
                        nicknameChangeTime = datetime.datetime.now(),
                        lastLoginIP = web.ctx.ip,
                        lastLoginTime = datetime.datetime.now()
                    )
                    last_user_id = users.get_douban_user_by_doubanid(douban_id).id
                    city = user_info.get('loc_name')
                    desc = user_info.get('desc')
                    users.update_profile(last_user_id, city = city, bio = desc )
                    session.douban_login(douban_id)
                    raise web.seeother(session.get_last_visited_url())
                elif c == 0:
                    session.douban_callback(user_info)
                    #返回 提醒用户需要激活邮件 的页面
                    raise web.seeother('/welcome/'+ user_info['uid'] +'/send_email_feedback?status=succesful')
            #如果没填写email
            else:
                session.douban_callback(user_info)
                #删除表中的记录 为了一会儿重新insert
                users.del_verification_data_by_douban_id(douban_id)
                #跳转到邮箱设置页面
                raise web.seeother('/welcome/'+ user_info['uid'])
        #如果是新用户
        else:
            session.douban_callback(user_info)
            #跳转到邮箱设置页面
            raise web.seeother('/welcome/'+ user_info['uid'])