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!
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()