django social auth facebook login

1.4k views Asked by At

The following code I have written is working correctly for some facebook users, but for some users its not... Can anyone tell me if I am writing in wrong pipeline order??

This is my Settings.py

LOGIN_URL          = '/dashboard/'
LOGIN_REDIRECT_URL = '/'
LOGIN_ERROR_URL    = '/dashboard/'
SOCIAL_AUTH_UID_LENGTH = 16
FACEBOOK_EXTENDED_PERMISSIONS = [ 'public_profile', 'email', 'user_location', 'user_hometown', 'user_birthday']
SOCIAL_AUTH_FACEBOOK_EXTRA_DATA = [ ('location', 'location'),('gender','gender'),('hometown', 'hometown')]

SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = True
SOCIAL_AUTH_CLEAN_USERNAMES = False



SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.associate.associate_by_email',
    'social_auth.backends.pipeline.user.get_username',
    'social_auth.backends.pipeline.user.create_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',
    'social_auth_login.pipeline.save_create_user', 


)



TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.i18n',
    'django.core.context_processors.request',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'cms.context_processors.media',
    'sekizai.context_processors.sekizai',
    'django.core.context_processors.debug',
    'social_auth.context_processors.social_auth_by_type_backends',
    'social_auth.context_processors.social_auth_by_name_backends',
    'social_auth.context_processors.social_auth_backends',
    'social_auth.context_processors.social_auth_login_redirect',
)

The following function is used for storing extra data:

def save_create_user(backend, user, response, *args, **kwargs):
    '''
    Store extra facebook data into customer model
    '''
    logger.debug(response)
    logger.debug(user)
    if backend.name == 'facebook':
        profile = user.get_profile()
        if profile is None:
            profile = Customer(user=user.username)
        url = 'https://graph.facebook.com/{0}/picture?redirect=false&access_token={1}'  #Picture 

My urls.py:

        profile_pic_url = dsa_urlopen(url.format( response.get('id'),response.get('access_token')))
        data = simplejson.load(profile_pic_url)
        profile.email = response.get('email')  # User email
        if data['data'] != '':
            if data['data']['is_silhouette'] == False:
                profile.image_url = data['data']['url']  # image url
            else:
                profile.image_url = ''   

        profile.facebook_id = response.get('id')  # facebook id       
    #profile.age = age_range      
    gender = response.get('gender')
    if gender != '':
          if gender =='male':
                user_gen ='Male'
          else:
                user_gen ='Female'
          profile.gender =user_gen  # gender
    profile.save()
1

There are 1 answers

1
gamer On

Try python-social-auth and have a look at this tutorial. I dont like posting the whole code.. http://www.artandlogic.com/blog/2014/04/tutorial-adding-facebooktwittergoogle-authentication-to-a-django-application/

Enjoy...