Search code examples
pythondjangofacebookdjango-socialauth

Django-Social-Auth Facebook doesn't save user's details


I'm using the basic Django-Social-Auth Facebook code to sign up via Facebook. for some reason, I can't get it to save the user's email, first and last name. it saves the username as the user's first+middle+last but the other attributes stay blank. I tried requesting the email address, and the app does say that we get the email address, but nothing saved to the tables.

I read all the posts and blogs about the Facebook sign up, but nothing seems to work for me. Am I missing something?

in settings.py I have:

FACEBOOK_EXTENDED_PERMISSIONS  = ['email']
SESSION_SERIALIZER='django.contrib.sessions.serializers.PickleSerializer'

AUTHENTICATION_BACKENDS = (
    'social_auth.backends.facebook.FacebookBackend',
    'django.contrib.auth.backends.ModelBackend',
)

INSTALLED_APPS = (
...
    'social_auth',
)

I was looking at auth\models.py, and maybe the create_user function doesn't get the email field.

def create_user(self, username, email=None, password=None, **extra_fields):
    return self._create_user(username, email, password, False, False,
                             **extra_fields)

Solution

  • I also had this same issue before, So you need to create a pipeline in your settings.py to update facebook user data. This code will help you to get facebook user data; http://django-social-auth.readthedocs.org/en/latest/pipeline.html

    def update_user_social_data(request, *args, **kwargs):
        user = kwargs['user']
        if not kwargs['is_new']:
            return
        user = kwargs['user']
        if kwargs['backend'].__class__.__name__ == 'FacebookBackend':
            fbuid = kwargs['response']['id']
            access_token = kwargs['response']['access_token']
    
            url = 'https://graph.facebook.com/{0}/' \
                  '?fields=email,gender,name' \
                  '&access_token={1}'.format(fbuid, access_token,)
    
            photo_url = "http://graph.facebook.com/%s/picture?type=large" \
                % kwargs['response']['id']
            request = urllib2.Request(url)
            response = urllib2.urlopen(request).read()
            email = json.loads(response).get('email')
            name = json.loads(response).get('name')
            gender = json.loads(response).get('gender')