为Django App实施Facebook注销功能

时间:2011-04-07 08:58:57

标签: python django django-models facebook

我创建了一个django应用程序,用户可以使用django应用程序用户名或facebook用户名进行登录。现在我想通过facebook登录时实现注销功能。有人可以帮我做这个。请帮我找一些解释这些的教程。我会在这里粘贴我的代码。

html facebook部分

{% load facebookconnect %}
{% facebook_connect_login_button %}
{% facebook_connect_script %}

facebookconnect.py

from django import template
from django.conf import settings
from django.core.urlresolvers import reverse

register = template.Library()

class FacebookScriptNode(template.Node):
        def render(self, context):
            return """
            <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>

            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

            <script type="text/javascript"> FB.init("%s", "%s");
                function facebook_onlogin() {
                    var uid = FB.Facebook.apiClient.get_session().uid;
                    var session_key = FB.Facebook.apiClient.get_session().session_key;
                    var expires = FB.Facebook.apiClient.get_session().expires;
                    var secret = FB.Facebook.apiClient.get_session().secret;
                    var sig = FB.Facebook.apiClient.get_session().sig;

                    fb_connect_ajax(expires, session_key, secret, uid, sig);

                }

                function fb_connect_ajax(expires, session_key, ss, user, sig) {

                    var post_string = 'expires=' + expires;
                    post_string = post_string + '&session_key=' + session_key;
                    post_string = post_string + '&ss=' + ss;
                    post_string = post_string + '&user=' + user;
                    post_string = post_string + '&sig=' + sig;

                    $.ajax({
                        type: "POST",
                        url: "%s",
                        data: post_string,
                        success: function(msg) {
                            window.location = '%s'; //.reload()
                        }
                    });
                } 
            </script>       
            """ % (settings.FACEBOOK_API_KEY, reverse('xd_receiver'), reverse('facebook_connect_ajax'), settings.LOGIN_REDIRECT_URL)


def facebook_connect_script(parser, token): return FacebookScriptNode()

register.tag(facebook_connect_script)

class FacebookLoginNode(template.Node):
    def render(self, context): 
        return "<fb:login-button onlogin='facebook_onlogin();'></fb:login-button>"
        #return "<fb:login-button onclick="openPopup('https://graph.facebook.com/oauth/authorize?client_id=a0acfd122e64fc21cfa34d47369f0c97&redirect_uri=http://mysite.com/mypage&display=popup');"></fb:login-button>"



def facebook_connect_login_button(parser, token): return FacebookLoginNode()

register.tag(facebook_connect_login_button)

在views.py中的功能

def login_facebook_connect(request):
    status = 'unknown failure'
    try:
        expires = request.POST['expires']
        ss = request.POST['ss']
        session_key = request.POST['session_key']
        user = request.POST['user']
        sig = request.POST['sig']

        pre_hash_string = "expires=%ssession_key=%sss=%suser=%s%s" % (
            expires,
            session_key,
            ss,
            user,
            settings.FACEBOOK_APPLICATION_SECRET,
        )
        post_hash_string = hashlib.new('md5')
        post_hash_string.update(pre_hash_string)
        if post_hash_string.hexdigest() == sig:
            try:
                fb = FacebookUser.objects.get(facebook_id=user)
                status = "logged in existing user"
            except FacebookUser.DoesNotExist:
                contrib_user = User()
                contrib_user.save()
                contrib_user.username = u"fbuser_%s" % contrib_user.id

                fb = FacebookUser()
                fb.facebook_id = user
                fb.contrib_user = contrib_user

                temp = hashlib.new('sha1')
                temp.update(str(datetime.datetime.now()))
                password = temp.hexdigest()

                contrib_user.set_password(password)
                fb.contrib_password = password
                fb.save()
                contrib_user.save()
                status = "created new user"

            authenticated_user = auth.authenticate(
                                         username=fb.contrib_user.username, 
                                         password=fb.contrib_password)
            auth.login(request, authenticated_user)
        else:
            status = 'wrong hash sig'

            logging.debug("FBConnect: user %s with exit status %s" % (user, status))

    except Exception, e:
        logging.debug("Exception thrown in the FBConnect ajax call: %s" % e)

    return HttpResponse("%s" % status)

def xd_receiver(request):
        return render_to_response('xd_receiver.html')

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

我使用了这个

<a href="http://m.facebook.com/logout.php?confirm=1&next=http://10.1.0.90:8080/registrationForm" >Sign Out</a>