def get_oauth_login_url(site): """ Get oauth login url according site """ from uliweb import settings from socialoauth import socialsites from socialoauth.utils import import_oauth_class socialsites._sites_id_name_table = {} socialsites.config(settings.SOCIALOAUTH) _s = import_oauth_class(socialsites[site])() return _s.authorize_url
def callback(site): from socialoauth import socialsites from socialoauth.utils import import_oauth_class from socialoauth.exception import SocialAPIError from uliweb.contrib.auth import login from uliweb.utils import date code = request.GET.get('code') if not code: # error occurred error("Can't found the code from oauth return data") socialsites._sites_id_name_table = {} socialsites.config(settings.SOCIALOAUTH) s = import_oauth_class(socialsites[site])() try: s.get_access_token(code) except SocialAPIError as e: print e.site_name print e.url print e.error_msg raise # we can get uid, name, avatar from s # we should store the user info to database User = functions.get_model('user') user = User.get((User.c.username==s.uid) & (User.c.login_type=='1') & (User.c.login_site==site)) if not user: user = User(username=s.uid, nickname=s.name, image=s.avatar, login_type='1', login_site=site) user.save() login(user) #引导用户填写邮箱地址 return redirect('/') else: user.last_login = date.now() user.save() login(user) return redirect('/')
from _bottle import Bottle, run, request, response, redirect, static_file CURRENT_PATH = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.normpath(os.path.join(CURRENT_PATH, '..'))) IMAGE_PATH = os.path.join(CURRENT_PATH, 'images') from settings import SOCIALOAUTH_SITES from socialoauth import socialsites from socialoauth.utils import import_oauth_class from socialoauth.exception import SocialAPIError from helper import Session, UserStorage, gen_session_id socialsites.config(SOCIALOAUTH_SITES) app = Bottle() @app.get('/static/images/<filepath:path>') def static_files(filepath): return static_file(filepath, IMAGE_PATH, mimetype='image/png') @app.get('/') def index(): session_id = request.get_cookie('session_id') if session_id:
from django.db.models import get_model from socialoauth import socialsites from socialoauth.utils import import_oauth_class from socialoauth.exception import SocialAPIError from .models import SocialUser from .app_settings import ( SOCIALOAUTH_SITES, SOCIAL_LOGIN_USER_INFO_MODEL, SOCIAL_LOGIN_DONE_REDIRECT_URL, SOCIAL_LOGIN_ERROR_REDIRECT_URL, ) socialsites.config(SOCIALOAUTH_SITES) def social_login_callback(request, sitename): code = request.GET.get('code', None) if not code: # Maybe user not authorize return HttpResponseRedirect(SOCIAL_LOGIN_ERROR_REDIRECT_URL) s = import_oauth_class(socialsites[sitename])() try: s.get_access_token(code) except SocialAPIError: # see social_oauth example and docs return HttpResponseRedirect(SOCIAL_LOGIN_ERROR_REDIRECT_URL)
# -*- coding: utf-8 -*- from flask import Blueprint from flask import render_template, url_for, request, session, redirect from socialoauth import socialsites from socialoauth.utils import import_oauth_class from socialoauth.exception import SocialAPIError app_name = __name__.split('.')[0] blueprint = Blueprint('index', __name__) socialsites.config({ 'facebook': ('{0}.providers.facebook.Facebook'.format(app_name), 1, 'Facebook', { 'redirect_uri': 'http://*****:*****@blueprint.route('/') def index(): user = session.get('user') return render_template('index.html', user=user) @blueprint.route('/login') def login(): connections = dict() for socialsite in socialsites.list_sites(): provider = import_oauth_class(socialsite)() connections[provider.site_name] = provider