def _save_user_and_token(token_dict, user_info, openid_type): ua = UserAlias.get(openid_type, user_info.get_user_id()) if not ua: if not g.user: ua = UserAlias.create_new_user(openid_type, user_info.get_user_id(), user_info.get_nickname()) else: ua = UserAlias.bind_to_exists_user(g.user, openid_type, user_info.get_user_id()) if not ua: return None ##设置个人资料(头像等等) u = User.get(ua.user_id) u.set_avatar_url(user_info.get_avatar()) u.set_icon_url(user_info.get_icon()) ##保存access token if openid_type == config.OPENID_TYPE_DICT[config.OPENID_TWITTER]: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("access_token_secret", "")) else: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("refresh_token", "")) ##set cookie,保持登录状态 if not g.user: g.user = User.get(ua.user_id) set_user_cookie(g.user, session) return g.user
def _save_user_and_token(token_dict, thirdparty_user, openid_type): first_connect = False ua = UserAlias.get(openid_type, thirdparty_user.get_user_id()) if not ua: if not g.user: ua = UserAlias.create_new_user(openid_type, thirdparty_user.get_user_id(), thirdparty_user.get_nickname()) else: ua = UserAlias.bind_to_exists_user(g.user, openid_type, thirdparty_user.get_user_id()) first_connect = True if not ua: return None ##设置个人资料(头像等等) u = User.get(ua.user_id) u.set_avatar_url(thirdparty_user.get_avatar()) u.set_icon_url(thirdparty_user.get_icon()) ##把各个第三方的uid保存到profile里面 k = openid_type v = { "uid": thirdparty_user.get_uid(), "name": thirdparty_user.get_nickname(), "intro": thirdparty_user.get_intro(), "signature": thirdparty_user.get_signature(), "avatar": thirdparty_user.get_avatar(), "icon": thirdparty_user.get_icon(), "email": thirdparty_user.get_email(), "first_connect": "Y" if first_connect else "N", } u.set_profile_item(k, json_encode(v)) ##保存access token if openid_type == config.OPENID_TYPE_DICT[config.OPENID_TWITTER]: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("access_token_secret", "")) else: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("refresh_token", "")) ##set cookie,保持登录状态 if not g.user: g.user = User.get(ua.user_id) set_user_cookie(g.user, session) return g.user