Facebook不会返回电子邮件Python社交身份验证

Has*_*ral 2 python django facebook django-socialauth python-social-auth

这是我的管道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',
)
Run Code Online (Sandbox Code Playgroud)

以及这一行:

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
Run Code Online (Sandbox Code Playgroud)

我也尝试过这个:

FACEBOOK_EXTENDED_PERMISSIONS = ['email']
Run Code Online (Sandbox Code Playgroud)

在我的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()
Run Code Online (Sandbox Code Playgroud)

但是,kwargs['response'].get('name')正确返回用户的全名,但kwargs['response'].get('email')始终为none.当我这样做时print kwargs['response'],它只显示uid和用户的名字,因此Facebook不会返回用户的电子邮件.怎么克服这个?谢谢!

mja*_*ews 10

我认为这可能是由于Facebook的Graph API v2.4的变化.

在以前版本的API中,添加

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
Run Code Online (Sandbox Code Playgroud)

将允许您获取Facebook用户的电子邮件.现在,这还不够,而且kwargs['response'].get('email')是空白的.

但是,正如python social auth issue 675中所解释的,还添加了这个

SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
    'fields': 'id,name,email', 
}
Run Code Online (Sandbox Code Playgroud)

您的设置似乎解决了这个问题,您可以再次收到用户的电子邮件.