Retrieving profile picture from google and facebook in python-social-auth

后端 未结 3 720
既然无缘
既然无缘 2021-02-03 12:31

How can I retrieve profile picture and date of birth from google and facebook using python-social-auth by extending pipeline? I\'ve read that I can make functions to do so and s

3条回答
  •  一整个雨季
    2021-02-03 12:54

    To get avatars from social login, you need create a pipeline.py file in your app and add this lines to 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',
        'social.pipeline.user.create_user',
        'social.pipeline.social_auth.associate_user',
        'social.pipeline.social_auth.load_extra_data',
        'social.pipeline.user.user_details',
        'apps.users.pipeline.get_avatar', # This is the path of your pipeline.py
        #and get_avatar is the function.
    )
    

    and later add this content to your pipeline.py file

    def get_avatar(backend, strategy, details, response,
            user=None, *args, **kwargs):
        url = None
        if backend.name == 'facebook':
            url = "http://graph.facebook.com/%s/picture?type=large"%response['id']
        if backend.name == 'twitter':
            url = response.get('profile_image_url', '').replace('_normal','')
        if backend.name == 'google-oauth2':
            url = response['image'].get('url')
            ext = url.split('.')[-1]
        if url:
            user.avatar = url
            user.save()
    

提交回复
热议问题