Exemplo n.º 1
0
def connect_authorized(app):
    code = request.args.get('code')
    open_user = {}
    if app == "sina":
        sinaClient = SinaAPIClient(
            app_key=current_app.config.get('SINA_APP_KEY'),
            app_secret=current_app.config.get('SINA_APP_SECRET'),
            redirect_uri=current_app.config.get('SINA_CALLBACK_URL')
        )
        try:
            r = sinaClient.request_access_token(code)
            access_token = r.get("access_token")
            expires_in = r.get("expires_in")
            sinaClient.set_access_token(access_token, expires_in)
            try:
                sinaUser = sinaClient.users.show.get(uid=r.uid)
                open_user["id"] = sinaUser.id
                open_user["name"] = sinaUser.name
                open_user["access_token"] = access_token
                open_user["figureurl"] = sinaUser.profile_image_url
            except:
                return redirect('/signin')
        except:
            return redirect
    elif app == "qq":
        qqClient = QQAPIClient(
            client_id=current_app.config.get('QQ_APP_KEY'),
            client_secret=current_app.config.get('QQ_APP_SECRET'),
            redirect_uri=current_app.config.get('QQ_CALLBACK_URL')
        )
        r = qqClient.request_access_token(code)
        access_token = r.get("access_token")
        expires_in = r.get("expires_in")
        qqClient.set_access_token(access_token, expires_in)
        openid = qqClient.request_openid()
        qqClient.set_openid(openid)
        qqUser = qqClient.request_api('user/get_user_info')
        open_user["id"] = openid
        open_user["name"] = qqUser.nickname
        open_user["access_token"] = access_token
        open_user["figureurl"] = qqUser.figureurl_1

    connect = Connect.query.filter(Connect.openid == open_user["id"], Connect.app == app).first()

    if connect:
        connect.access_token = open_user["access_token"]
        db.session.commit()
        user = User.query.get(connect.user_id)
        utils.login(user)
        return redirect(url_for("frontend.index"))
    else:
        session['user'] = User(
            nickname=open_user["name"],
            name=open_user["name"],
            join=utils.get_now(),
            figureurl=open_user["figureurl"]
        )
        session['connect'] = Connect(open_user["id"], open_user["access_token"], app)
        return redirect(url_for("frontend.connect_callback", app=app))
Exemplo n.º 2
0
def connect_sina(app):
    url = ''
    if app == 'sina':
        sinaClient = SinaAPIClient(
            app_key=current_app.config.get('SINA_APP_KEY'),
            app_secret=current_app.config.get('SINA_APP_SECRET'),
            redirect_uri=current_app.config.get('SINA_CALLBACK_URL')
        )
        url = sinaClient.get_authorize_url()
    elif app == 'qq':
        qqClient = QQAPIClient(
            client_id=current_app.config.get('QQ_APP_KEY'),
            client_secret=current_app.config.get('QQ_APP_SECRET'),
            redirect_uri=current_app.config.get('QQ_CALLBACK_URL')
        )
        url = qqClient.get_auth_url()
    return redirect(url)