Exemplo n.º 1
0
def facebook_connect(request):
    """
    Create or connect a user via facebook connect.
    """
    try:
        access_token = request.POST["access_token"]
        user_id      = request.POST["id"]
        request_sig = request.POST["sig"]

        sig, expected_sig = get_sig_and_expected_sig(request_sig, settings.FACEBOOK_APP_SECRET)

        if sig == expected_sig:
            try:
                f_user = FacebookUser.objects.get(facebook_id=user_id)

            except FacebookUser.DoesNotExist:

                # Facebook api to get the user profile
                profile = facebook.GraphAPI(access_token).get_object('me')

                first_name  = profile["first_name"]
                last_name   = profile["last_name"]

                # Create the user
                user = User()
                user.save()

                user.username = u"fbuser_%s" % user.id
                user.first_name = first_name
                user.last_name = last_name

                # Attempt to set the email
                try:
                    if "email" in settings.FACEBOOK_SCOPE:
                        email = profile["email"]
                        user.email = email
                except AttributeError:
                    pass

                # Create the facebook user
                f_user = FacebookUser(facebook_id=user_id,
                                      contrib_user=user)

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

                user.set_password(password)
                f_user.contrib_password = password

                # Save
                f_user.save()
                user.save()
                        
            # Authenticate and login
            authenticated_user = auth.authenticate(username=f_user.contrib_user.username,
                                                   password=f_user.contrib_password)
            auth.login(request, authenticated_user)

        else:

            content = {
                "is_error" : True,
                "error_text" : "Error connecting facebook connect user %s " % (user_id)
            }

            return json_response(content, 200)
            
    except Exception, e:
        content = {
            "is_error" : True,
            "error_text" : "Error in ajax call, exception: %s" % e
        }

        return json_response(content, 200)
Exemplo n.º 2
0
def facebook_connect(request):
	"""
	Create or connect a user via facebook connect.
	"""
	try:
		access_token = request.POST["access_token"]
		user_id	  = request.POST["id"]
		request_sig = request.POST["sig"]

		sig, expected_sig = get_sig_and_expected_sig(request_sig, settings.FACEBOOK_APP_SECRET)

		if sig == expected_sig:
			try:
				f_user = FacebookUser.objects.get(facebook_id=user_id)
			
			except FacebookUser.DoesNotExist:

				# Facebook api to get the user profile
				profile = facebook.GraphAPI(access_token).get_object('me')

				first_name  = profile["first_name"]
				last_name   = profile["last_name"]

				# Create the user
				user = User()
				user.save()

				user.username = u"fbuser_%s" % user.id
				user.first_name = first_name
				user.last_name = last_name

				# Attempt to set the email
				try:
					if "email" in settings.FACEBOOK_SCOPE:
						email = profile["email"]
						user.email = email
				except AttributeError:
					pass

				# Create the facebook user
				f_user = FacebookUser(facebook_id=user_id, contrib_user=user)

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

				user.set_password(password)
				f_user.contrib_password = password

				# Save
				f_user.save()
				user.save()
				
			# Authenticate and login
			#service = OAuth2Service.objects.get(name='facebook')
			try:
				oauth2 = OAuth2Access.objects.get(user_id=f_user.contrib_user_id, token_type=TOKEN_ACCESS, service='facebook')
			except ObjectDoesNotExist, e:
				oauth2 = OAuth2Access()
				oauth2.token = request.POST['access_token']
				oauth2.token_type = TOKEN_ACCESS
				oauth2.user_id = f_user.contrib_user_id
				oauth2.service = 'facebook'
				oauth2.save()
				
				task = taskqueue.Task(url='/oauth2/facebook/task/connections/' + str(f_user.contrib_user_id))
				task.add()
			
			authenticated_user = auth.authenticate(username=f_user.contrib_user.username,
							password=f_user.contrib_password)
			auth.login(request, authenticated_user)

		else: