i have created a django app, which has a facebook login option also. For doing it i was following this link django-facebook connect with ajax. I did everything as said by the link, and i am getting the user signed in with facebook connect. But after the user logs in from 'registrationForm'(FB log in button given) page, the outh page pop ups to enter username and password. when giving it the user is signed in but the same registrationForm comes again without redirecting the page. Can somebody help me to set a callback url after the FB logs in is success. for FB connect i am using the same code as given in the above site.
facebookconnect.py
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>"
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')
in settings.py
LOGIN_REDIRECT_URL = '/forgotPassword/'
html login part [registrationForm]
<p>Login via facebook!</p>
{% load facebookconnect %}
{% facebook_connect_login_button %}
{% facebook_connect_script %}
No comments:
Post a Comment