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

后端 未结 3 774
既然无缘
既然无缘 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:45

    Here's what I have used to save pictures for Facebook:

    def save_profile_picture(backend, user, response, details,
                             is_new=False,*args,**kwargs):
    
        if backend.__class__.__name__ == 'FacebookOAuth2':
            up = UserProperties.objects.get_or_create(user=user) #RETURNS TUPLE (instance, created(boolean))
            if not up[0].photo:
                url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
                response = urllib.request.urlopen(url)
                io = BytesIO(response.read())
                up[0].photo.save('profile_pic_{}.jpg'.format(user.pk), File(io))
                up[0].save() 
    

    Save this function into a file, for instance, pipelines.py, and then add the function to your SOCIAL_AUTH_PIPELINE in your settings.

    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', 
        'social.pipeline.user.create_user',
        'social.pipeline.social_auth.associate_user',
        'social.pipeline.social_auth.load_extra_data',
        'social.pipeline.user.user_details',
        'projects.pipeline.save_profile_picture', #save facebook profile image,
    )
    

    For Facebook you need to create your own Facebook app. You can only retrieve information and pictures from users who have given you the permission to do so. Same rules more or less apply for Google. Read their API docs for more detail.

提交回复
热议问题