Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Doesn't Return Email Python Social Auth

here's my pipeline in settings.py:

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'accounts.pipeline.create_user',
    'accounts.pipeline.update_user_social_data',
    # 'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
)

along with this line:

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

I tried this as well:

FACEBOOK_EXTENDED_PERMISSIONS = ['email']

and in my pipeline.py:

def create_user(strategy, details, user=None, *args, **kwargs):
    if user:
        return {
            'is_new': False
        }

    fields = dict((name, kwargs.get(name) or details.get(name))
                  for name in strategy.setting('USER_FIELDS',
                                               USER_FIELDS))

    if not fields:
        return

    if strategy.session_get('signup') == 'UserType1':
        user1 = UserType1.objects.create_user(username=fields.get('username'))
        user1.user_type = User.USERTYPE1
        user1.save()

        return {
            'is_new': True,
            'user': user1
        }

    elif strategy.session_get('signup') == 'UserType2':
        user2 = UserType2.objects.create_user(username=fields.get('username'))
        user2.user_type = User.USERTYPE2
        user2.save()

        return {
            'is_new': True,
            'user': user2
        }


def update_user_social_data(strategy, *args, **kwargs):
    if not kwargs['is_new']:
        return

    full_name = ''
    backend = kwargs['backend']

    user = kwargs['user']

    full_name = kwargs['response'].get('name')
    user.full_name = full_name

    email = kwargs['response'].get('email')
    user.email = email

    user.save()

However, kwargs['response'].get('name') returns user's full name correctly, but kwargs['response'].get('email') is always none. When I do print kwargs['response'], it only shows uid and the name of the user as well, so Facebook doesn't return user's email. How to overcome this? Thanks!

like image 610
Hasan Can Saral Avatar asked Dec 11 '22 21:12

Hasan Can Saral


1 Answers

I think this might be due to changes in Facebook's Graph API v2.4.

In previous versions of the API, adding

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

would allow you to get the facebook user's email. Now, that is not sufficient and kwargs['response'].get('email') is blank.

However, as explained in python social auth issue 675, also adding this

SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
    'fields': 'id,name,email', 
}

to your settings seems to get around this problem and you can get the user's email again.

like image 147
mjandrews Avatar answered Dec 13 '22 10:12

mjandrews