Hasan Can Saral
Hasan Can Saral

Reputation: 3288

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!

Upvotes: 4

Views: 3272

Answers (3)

Rishabh
Rishabh

Reputation: 39

Facebook does not sends email-ids to localhost:8000. Check out the documentation here: python-django social auth for FB

Upvotes: -1

mjandrews
mjandrews

Reputation: 2464

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.

Upvotes: 13

Hasan Can Saral
Hasan Can Saral

Reputation: 3288

Apparently you need to make another request like following (again in social pipeline):

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

    user = kwargs['user']

    fbuid = kwargs['response']['id']
    access_token = kwargs['response']['access_token']

    url = u'https://graph.facebook.com/{0}/' \
          u'?fields=email' \
          u'&access_token={1}'.format(
        fbuid,
        access_token,
    )

    request = urllib2.Request(url)
    email = json.loads(urllib2.urlopen(request).read()).get('email')

    user.email = email
    user.save()

Upvotes: 3

Related Questions