本文整理汇总了Python中daimaduan.models.base.User.find_by_oauth方法的典型用法代码示例。如果您正苦于以下问题:Python User.find_by_oauth方法的具体用法?Python User.find_by_oauth怎么用?Python User.find_by_oauth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类daimaduan.models.base.User
的用法示例。
在下文中一共展示了User.find_by_oauth方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: oauth_callback
# 需要导入模块: from daimaduan.models.base import User [as 别名]
# 或者: from daimaduan.models.base.User import find_by_oauth [as 别名]
def oauth_callback(provider):
current_app.logger.info("Oauth callback for %s" % provider)
redirect_uri = current_app.config['OAUTH'][provider]['callback_url']
oauth_service = get_oauth_services()[provider]
data = dict(code=request.args.get('code'),
grant_type='authorization_code',
redirect_uri=redirect_uri)
if provider == 'google':
oauth_session = oauth_service.get_auth_session(data=data, decoder=json.loads)
user_info = oauth_session.get('userinfo').json()
email = session['email'] = user_info['email']
username = user_info['given_name']
elif provider == 'github':
oauth_session = oauth_service.get_auth_session(data=data)
user_info = oauth_session.get('user').json()
email = session['email'] = user_info['email']
username = user_info['login']
access_token = oauth_session.access_token
user_info['id'] = str(user_info['id'])
current_app.logger.info("%s oauth access token is: %s" % (provider, access_token))
current_app.logger.info("%s oauth user info is %s" % (provider, user_info))
user = User.find_by_oauth(provider, user_info['id'])
if user:
# TODO: 直接登录时更新 token.
user_mixin = LoginManagerUser(user)
login_user(user_mixin)
flash(u"登录成功", category='info')
return redirect('/')
else:
user = User.objects(email=email).first()
if user:
user_oauth = UserOauth(provider=provider, openid=user_info['id'], token=access_token)
user_oauth.save()
user_mixin = LoginManagerUser(user)
login_user(user_mixin)
flash(u"登录成功", category='info')
return redirect('/')
else:
return render_template('users/finish_signup.html',
form=UserInfoForm(email=email, username=username))
示例2: oauth_callback
# 需要导入模块: from daimaduan.models.base import User [as 别名]
# 或者: from daimaduan.models.base.User import find_by_oauth [as 别名]
def oauth_callback(provider):
logger.info("Oauth callback for %s" % provider)
redirect_uri = app.config['oauth.%s.callback_url' % provider]
oauth_service = oauth_services[provider]
session = get_session(request)
data = dict(code=request.params.get('code'),
grant_type='authorization_code',
redirect_uri=redirect_uri)
if provider == 'google':
oauth_session = oauth_service.get_auth_session(data=data, decoder=json.loads)
user_info = oauth_session.get('userinfo').json()
email = session['email'] = user_info['email']
username = user_info['given_name']
elif provider == 'github':
oauth_session = oauth_service.get_auth_session(data=data)
user_info = oauth_session.get('user').json()
email = session['email'] = user_info['email']
username = user_info['login']
access_token = oauth_session.access_token
user_info['id'] = str(user_info['id'])
logger.info("%s oauth access token is: %s" % (provider, access_token))
logger.info("%s oauth user info is %s" % (provider, user_info))
user = User.find_by_oauth(provider, user_info['id'])
if user:
# TODO: 直接登录时更新 token.
login.login_user(str(user.id))
return redirect('/')
else:
user = User.objects(email=email).first()
if user:
user_oauth = UserOauth(provider=provider, openid=user_info['id'], token=access_token)
user_oauth.save()
login.login_user(str(user.id))
return redirect('/')
else:
return {'form': UserInfoForm(email=email, username=username), 'token': request.csrf_token}