Example #1
0
def test_login(request, fb_id):
    from facebook.models import FacebookProfile

    try:
        fb_user = FacebookProfile.objects.get(facebook_id=fb_id)
        user = fb_user.user
    except FacebookProfile.DoesNotExist:
        user = User.objects.create_user(fb_profile['id'],
                                        fb_profile['email'])
        user.first_name = fb_profile['first_name']
        user.last_name = fb_profile['last_name']

        # Facebook allows for longer name. This fixes the inconsistencies between
        # Django and Postgres
        if len(user.first_name) > 30:
            user.first_name = user.first_name[:30]
        if len(user.last_name ) > 30:
            user.last_name = user.last_name[:30]

                # with django-primate User has one field called 'name' instead
                # of first_name and last_name
        user.name = u'%s %s' % (user.first_name, user.last_name)
        user.save()
        
        # Create the FacebookProfile
        fb_user = FacebookProfile(user=user,
                                  facebook_id=fb_profile['id'],
                                  access_token=access_token)
        fb_user.save()

    fb_user.user.backend = 'django.contrib.auth.backends.ModelBackend' 
    auth_login(request, fb_user.user)
    url = getattr(settings, "LOGIN_REDIRECT_URL", "/")
    resp = HttpResponseRedirect(url)
    return resp
Example #2
0
    def authenticate(self, user_id=None, access_token=None):
        data = None
        if user_id is None:
            data = urllib.urlopen(
                'https://graph.facebook.com/me?access_token=%s' % access_token)
            data = json.load(data)
            user_id = data[u'id']

        try:
            # try to find the user
            fb_user = FacebookProfile.objects.get(facebook_id=user_id)
            user = fb_user.user

            # update access_token
            fb_user.access_token = access_token
            fb_user.save()

        except FacebookProfile.DoesNotExist:
            # user does not exist yet, get some more data and create a new user
            if data is None:
                data = urllib.urlopen(
                    'https://graph.facebook.com/me?access_token=%s' %
                    access_token)
                data = json.load(data)
                user_id = data[u'id']

            # use Facebook username if possible, but not all Facebook users
            # have usernames. In that case we still ensure the usernames are
            # unique
            username = ""
            email = ""
            if 'username' in data:
                username = data['username']
                if 'email' in data:
                    email = data['email']
                else:
                    email = data['username'] + "@facebook.com"
            elif 'email' in data:
                username = data['email'].split('@')[0] + url_safe_encode(
                    long(user_id))
                email = data['email']
            else:
                username = "******" + url_safe_encode(long(user_id))
                email = username + '@facebook.com'

            user = User.objects.create_user(username, email)
            user.first_name = data['first_name']
            user.last_name = data['last_name']
            user.save()

            # Create the FacebookProfile
            fb_user = FacebookProfile(user=user,
                                      facebook_id=user_id,
                                      access_token=access_token)
            fb_user.save()

        return user
Example #3
0
    def authenticate(self, token=None, request=None):
        """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """
        args = {
            'client_id':
            settings.FACEBOOK_APP_ID,
            'client_secret':
            settings.FACEBOOK_APP_SECRET,
            'redirect_uri':
            request.build_absolute_uri('/facebook/authentication_callback'),
            'code':
            token,
        }

        # Get a legit access token
        target = urllib.urlopen(
            'https://graph.facebook.com/oauth/access_token?' +
            urllib.urlencode(args)).read()
        response = cgi.parse_qs(target)
        access_token = response['access_token'][-1]

        # Read the user's profile information
        fb_profile = urllib.urlopen(
            'https://graph.facebook.com/me?access_token=%s' % access_token)
        fb_profile = json.load(fb_profile)

        try:
            # Try and find existing user
            fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id'])
            user = fb_user.user

            # Update access_token
            fb_user.access_token = access_token
            fb_user.save()

        except FacebookProfile.DoesNotExist:
            # No existing user, create one

            username = fb_profile.get('username', fb_profile['email'].split(
                '@')[0])  # Not all users have usernames
            try:
                user = User.objects.create_user(username, fb_profile['email'])
            except IntegrityError:
                # Username already exists, make it unique
                user = User.objects.create_user(username + fb_profile['id'],
                                                fb_profile['email'])
            user.first_name = fb_profile['first_name']
            user.last_name = fb_profile['last_name']
            user.save()

            # Create the FacebookProfile
            fb_user = FacebookProfile(user=user,
                                      facebook_id=fb_profile['id'],
                                      access_token=access_token)
            fb_user.save()

        return user
Example #4
0
    def authenticate(self, token=None, request=None):
        """ Reads in a Facebook code and asks Facebook if it's valid and what
        user it points to. """
        args = {
            'client_id':
            settings.FACEBOOK_APP_ID,
            'client_secret':
            settings.FACEBOOK_APP_SECRET,
            'redirect_uri':
            request.build_absolute_uri(reverse('facebook-callback')),
            'code':
            token,
        }

        # Get a legit access token
        target = urllib.urlopen(
            'https://graph.facebook.com/oauth/access_token?' +
            urllib.urlencode(args)).read()
        response = cgi.parse_qs(target)
        access_token = response['access_token'][-1]

        # Read the user's profile information
        fb_profile = urllib.urlopen(
            'https://graph.facebook.com/me?access_token=%s' % access_token)
        fb_profile = json.load(fb_profile)

        try:
            # Try and find existing user
            fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id'])
            user = fb_user.user
            # Update access_token
            fb_user.access_token = access_token
            fb_user.save()
        except FacebookProfile.DoesNotExist:
            # See if email has already been registered
            try:
                user = User.objects.get(email=fb_profile['email'])
            except User.DoesNotExist:
                # No existing user, create one
                user = User.objects.create_user(fb_profile['id'],
                                                fb_profile['email'])
                user.first_name = fb_profile['first_name']
                user.last_name = fb_profile['last_name']
                # with django-primate User has one field called 'name' instead
                # of first_name and last_name
                user.name = u'%s %s' % (user.first_name, user.last_name)
                user.save()

            # Create the FacebookProfile
            fb_user = FacebookProfile(user=user,
                                      facebook_id=fb_profile['id'],
                                      access_token=access_token)
            fb_user.save()
        return user
Example #5
0
    def authenticate(self, request=None, token=None):
        """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """
        args = {
            'client_id':
            settings.FACEBOOK_APP_ID,
            'client_secret':
            settings.FACEBOOK_APP_SECRET,
            'redirect_uri':
            request.build_absolute_uri(reverse('facebook_login_callback')),
            'code':
            token,
        }

        # Get a legit access token
        target = urllib.urlopen(
            'https://graph.facebook.com/oauth/access_token?' +
            urllib.urlencode(args)).read()
        response = cgi.parse_qs(target)
        access_token = response['access_token'][-1]

        # Read the user's profile information
        fb_profile = urllib.urlopen(
            'https://graph.facebook.com/me?access_token=%s' % access_token)
        fb_profile = json.load(fb_profile)

        try:
            # Try and find existing user
            facebook_profile = FacebookProfile.objects.get(
                facebook_id=fb_profile['id'])

            # Update access_token
            facebook_profile.access_token = access_token
            facebook_profile.save()

        except FacebookProfile.DoesNotExist:
            # No existing user, create one
            facebook_profile = FacebookProfile(facebook_id=fb_profile['id'],
                                               access_token=access_token)
            facebook_profile.save()
        backend = facebook_profile.getLoginBackend(request)

        try:
            fb_username = fb_profile['username']
            fb_mail = fb_profile['email']
            user = backend.login(facebook_profile,
                                 related_name='facebook_profile',
                                 username=fb_profile['username'],
                                 email=fb_profile['email'])
        except:
            import sys
            print "error ", sys.exc_info()[0]
        return user
Example #6
0
    def authenticate(self, token=None, request=None):
        """ Reads in a Facebook code and asks Facebook if it's valid and what
        user it points to. """
        args = {
            'client_id':
            settings.FACEBOOK_APP_ID,
            'client_secret':
            settings.FACEBOOK_APP_SECRET,
            'redirect_uri':
            request.build_absolute_uri(reverse('facebook-callback')),
            'code':
            token,
        }

        # Get a legit access token
        target = urllib.urlopen(
            'https://graph.facebook.com/oauth/access_token?' +
            urllib.urlencode(args)).read()
        response = cgi.parse_qs(target)
        access_token = response['access_token'][-1]

        # Read the user's profile information
        fb_profile = urllib.urlopen(
            'https://graph.facebook.com/me?access_token=%s' % access_token)
        fb_profile = json.load(fb_profile)

        try:
            # Try and find existing user
            fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id'])
            user = fb_user.user
            # Update access_token
            fb_user.access_token = access_token
            fb_user.save()
        except FacebookProfile.DoesNotExist:
            # No existing user
            if getattr(settings, 'FACEBOOK_FORCE_SIGNUP', False):
                # No existing user, use anonymous
                user = AnonymousUser()
                user.username = username
                user.first_name = fb_profile['first_name']
                user.last_name = fb_profile['last_name']
                fb_user = FacebookProfile(facebook_id=fb_profile['id'],
                                          access_token=access_token)
                user.facebookprofile = fb_user
            else:
                # No existing user, create one
                user = User.objects.create_user(fb_profile['id'],
                                                fb_profile['email'])
                user.first_name = fb_profile['first_name']
                user.last_name = fb_profile['last_name']

                # Facebook allows for longer name. This fixes the inconsistencies between
                # Django and Postgres
                if len(user.first_name) > 30:
                    user.first_name = user.first_name[:30]
                if len(user.last_name) > 30:
                    user.last_name = user.last_name[:30]

                # with django-primate User has one field called 'name' instead
                # of first_name and last_name
                user.name = u'%s %s' % (user.first_name, user.last_name)
                user.save()

                # Create the FacebookProfile
                fb_user = FacebookProfile(user=user,
                                          facebook_id=fb_profile['id'],
                                          access_token=access_token)
                fb_user.save()
        return user