django social auth使用facebook数据

fre*_*row 4 authentication django social facebook

我设法安装了django-social-auth,但我错过了我可以实际使用从facebook获得的数据以创建用户名并可能使用个人资料图片或关于字段的那一刻......

我知道在django-social-auth的设置中我可以设置SOCIAL_AUTH_DEFAULT_USERNAME ='new_social_auth_user'和FACEBOOK_EXTENDED_PERMISSIONS = [...]但是我不会在我的代码中取消我可以插入这些数据的地方,所以不要随意用户名.

Tim*_*ony 13

django-social-auth实现一个管道(这似乎是一个新功能,因为它在我尝试时不存在),允许您在身份验证过程中的某些步骤插入自定义函数.查看此处的文档,以及此处示例pipline函数.

所以你可以编写一个函数:

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.associate.associate_by_email',
    'social_auth.backends.pipeline.user.get_username',

    'app.pipeline.custom_create_user',

    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details'
)
Run Code Online (Sandbox Code Playgroud)

您的custom_create_user函数包装默认值create_user并根据您自己的需要创建用户名:

from social_auth.backends.pipeline.user import create_user
def custom_create_user(request, *args, **kwargs):
     user = *kwargs.get('user', None)
     # Do something with username
     return create_user(request, args, kwargs)
Run Code Online (Sandbox Code Playgroud)