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))
def connect_callback(): """完善资料""" if request.method == "GET": siteName = u"新浪微博" if session.get('connect').app != "qq" else u"腾讯QQ" return render_template("callback.html", user=session.get('user'), app=session.get('connect').app, siteName=siteName) elif request.method == "POST": nickname = request.form["nickname"] name = request.form["name"] user = session.get('user') if name: user.name = name if nickname: user.nickname = nickname user.connects = [session.get('connect')] db.session.add(user) db.session.commit() utils.login(user) return redirect(url_for("frontend.index", action="signup_success"))