坚持在django写facebook认证

时间:2012-01-29 10:44:28

标签: django facebook authentication

这是我从django RemoteUserBackend中得到启发的代码,它还没有完整,我不知道正常后端或remoteuserbackend中的哪个地方确实调用了authenticate方法?对不起,我是django的新手,正在进行的用户日志似乎像 magic

一样工作
from django.contrib import ModelBackend      
from django.contrib.auth.models import User, Permission

def facebook_login_required(orig_view):
    def wrapper(request):
        if not request.user.is_authenticated():
            redirect_url = 'https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&scope=email,read_stream'%(SETTINGS.FB_APPID,request.getlocation)
            HttpResponseRedirect(redirect_url)
        else:
            # user is logged in, its safe to process the view
            return orig_view
    return wrapper

class FacebookAuthBackend(ModelBackend):
    def authenticate(self,userid):
        """
        The ``userid`` passed here is considered trusted.This method
        simply returns the ``User`` objects with the given id, else
        it creates a new user with the this ``userid`` if the it does
        not existz
        """
        if not userid:
            user = User(userid=userid)
            user.save()
        user = None
        try:
            user = User.objects.get(userid=userid)
        except User.DoesNotExist:
            pass
        return user

    def get_user(self,userid):
        try:
            User.objects.get(userid=userid)
        except User.DoesNotExist:
            return None

1 个答案:

答案 0 :(得分:0)

嗯,你可以随时查看源代码并弄清楚工作原理和你的工作原理

https://github.com/omab/django-social-auth/blob/master/social_auth/backends/facebook.py