1

我正在创建一个网站,可以选择使用 facebook、google 等登录(比如 stackoverflow.com,它可以选择以 google 用户等身份登录)。

我的问题是

  • 如何管理用户帐户。(我的意思是用户活动)

我的用户数据库是

注册用户/models.py

class Employer(models.Model):
    user=models.OneToOneField(User)
    companyname= models.CharField(max_length=100,blank=True)
    leader=models.BooleanField(default=True)
    #avatar = models.ImageField("Profile Pic", upload_to="images/", blank=True, null=True)    
    def __unicode__(self):
        return self.user.username


class Jobseeker(models.Model):
    user=models.OneToOneField(User)
    #avatar = models.ImageField("Profile Pic", upload_to="images/", blank=True, null=True)
    def __unicode__(self):
        return self.user.username

fblogin.html

 <html>     
 <head>       
 <title>My Facebook Login Page with listener</title>     
 </head>     
<body>       
 <div id="fb-root"></div>          
 <div class="box">
    <div class="info">
        This is an example of subscribing to the auth login event
    </div>
    <div class="fb-login-button">Login with Facebook</div>     
 </div>     
 <script>

window.fbAsyncInit = function() {
FB.init({
    appId: '*************',
    status: true,
    cookie: true,
    xfbml: true,
    oauth: true
});

FB.Event.subscribe('auth.login', function(response) {
    // {
    //   status: "",         /* Current status of the session */
    //   authResponse: {          /* Information about the current session */
    //      userID: ""          /* String representing the current user's ID */
    //      signedRequest: "",  /* String with the current signedRequest */
    //      expiresIn: "",      /* UNIX time when the session expires */
    //      accessToken: "",    /* Access token of the user */
    //   }
    // }

    // alert('event status: ' + response.status);
});

FB.getLoginStatus(function(response) {
    //  {
    //     status: 'connected',
    //     authResponse: {
    //        accessToken: '...',
    //        expiresIn:'...',
    //        signedRequest:'...',
    //        userID:'...'
    //     }
    //  }

    //alert('getLoginStatus: ' + response.status);

    if (response.status=='connected') {
        FB.api('/me',function(response){

             var field_name=$(this).attr("id");
            var field_val=$(this).val();
           var params ={ param1:response.name, response.id };

           $.ajax({ url: "/createfbuser/",
            dataType: "json",
            data: params,           
            success: setResult      
           });                
        });
    }
});

};
(function(d) {
    var js, id = 'facebook-jssdk';
    if (d.getElementById(id)) {
    return;
    }
    js = d.createElement('script');
    js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    d.getElementsByTagName('head')[0].appendChild(js);
    }(document));
</script>
</body>  

</html>

我需要添加一些类似的东西吗

注册用户/models.py

......other models(listed above)

class userFacebook(models.Model):
    user=models.OneToOneField(User)

并在第一次 facebook 登录后创建用户

网址.py

.....other urls

url(r'^createfbuser/$', 'create_facebook_user_ajax'),

注册用户/views.py

def create_facebook_user_ajax(request):
    f_name=request.GET['param1']
    f_id=request.GET['param2']        
    User.objects.create_user(##########what are the things i want to provide here.)
    #if i provide the facebook username then there may be already a user with same username

或者有没有其他方法。。

对不起,我是 facebook 登录的新手。

4

1 回答 1

1

django-social-auth是一个非常容易安装的解决方案,可以满足您的要求。它们是其他 django 应用程序,您可以在hackerluddite 的博客上了解它。

引用 github 自述文件 django-social-auth 存储库

Django 社交认证

Django Social Auth 是一种为 Django 项目设置社交认证/授权机制的简单方法。

使用来自 django-twitter-oauth 和 django-openid-auth 的基本代码制作,它实现了一个通用接口来定义来自第三方的新身份验证提供程序。

不要忘记启动 github repo。

于 2013-08-22T12:50:18.793 回答