how to get user email with python social auth with facebook and save it
Asked Answered
L

4

17

I'm trying to implement python-social-auth in django.

I want users to authenticate through facebook and save their email.

I'm able to authenticate users but the extended permission for email is not showing up in the facebook authentification box and it's not storing the email in the database.

In settings.py I have the follwoing:

SOCIAL_AUTH_FACEBOOK_KEY='xxx'
SOCIAL_AUTH_FACEBOOK_SECRET='xxx'
FACEBOOK_EXTENDED_PERMISSIONS = ['email']

AUTHENTICATION_BACKENDS = (
    'social.backends.facebook.FacebookOAuth2',
    'social.backends.email.EmailAuth',
    'django.contrib.auth.backends.ModelBackend',
)

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/done/'
LOGOUT_REDIRECT_URL = '/'
URL_PATH = ''
SOCIAL_AUTH_STRATEGY = 'social.strategies.django_strategy.DjangoStrategy'
SOCIAL_AUTH_STORAGE = 'social.apps.django_app.default.models.DjangoStorage'

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',
    'social.pipeline.social_auth.associate_by_email',
    # 'users.pipeline.require_email',
    'social.pipeline.mail.mail_validation',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details'
)

The facebook dialog box...

enter image description here

How can I solve this?

Loser answered 23/2, 2014 at 12:9 Comment(0)
G
56

After some changes in Facebook Login API - Facebook's Graph API v2.4 You will have to add these lines to fetch email

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
    'fields': 'id,name,email', 
}
Gibbons answered 7/1, 2016 at 9:27 Comment(4)
Yep.. You need the EXTRA_PARAMS alsoVervain
Perfect, this updates users in my db when they log back in.Pathological
Does this still work? I can't get it working and the worst thing is that it doesn't even produce any error logs, it just silently skips the email retrieval step.Myrtismyrtle
Email not being retrieve not even with this settingParticularize
T
14

I think the problem is using FACEBOOK_EXTENDED_PERMISSIONS.

According to http://python-social-auth.readthedocs.org/en/latest/backends/facebook.html#oauth2 you should use:

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

Tuba answered 23/2, 2014 at 12:45 Comment(2)
Does it still work by any chance? I can't get it working.Horvath
You need the EXTRA_PARAMS also. See answer below.Vervain
H
0

Add this to your settings.py file

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
Harod answered 19/8, 2015 at 3:26 Comment(0)
N
0

Are you doing it on the test website?

Re-read the documentation please: [https://python-social-auth.readthedocs.io/en/latest/backends/facebook.html#oauth2][1]

It says:

If you define a redirect URL in Facebook setup page, be sure to not define http://127.0.0.1:8000 or http://localhost:8000 because it won’t work when testing

Natividad answered 30/9, 2019 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.