使用python-social-auth在模型中保存Facebook个人资料图片 [英] Save facebook profile picture in model using python-social-auth

查看:102
本文介绍了使用python-social-auth在模型中保存Facebook个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何存储通过Facebook登录并保存在我的用户资料模型中的用户的Facebook个人资料图片。

How to store the get Facebook profile picture of a user while logging in through Facebook and saving it in my userprofile model.

我发现这个链接说明如何请使用django-social-auth, https://gist.github.com/kalamhavij/1662930。但是信号现在已被弃用,我必须使用管道。

I found this link which says how to do so using django-social-auth, https://gist.github.com/kalamhavij/1662930. but signals is now deprecated and I have to use pipeline.

任何想法如何使用python-social-auth和管道做同样的事情?

Any idea how can I do the same using python-social-auth and pipeline?

推荐答案

这是如何与我一起工作的。 (来自 https://github.com/omab/python-social-auth/issues / 80

This is how it worked with me. (from https://github.com/omab/python-social-auth/issues/80)

将以下代码添加到pipeline.py:

Add the following code to pipeline.py:

from requests import request, HTTPError

from django.core.files.base import ContentFile


def save_profile_picture(strategy, user, response, details,
                         is_new=False,*args,**kwargs):

    if is_new and strategy.backend.name == 'facebook':
        url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])

        try:
            response = request('GET', url, params={'type': 'large'})
            response.raise_for_status()
        except HTTPError:
            pass
        else:
            profile = user.get_profile()
            profile.profile_photo.save('{0}_social.jpg'.format(user.username),
                                   ContentFile(response.content))
            profile.save()

并添加到管道中settings.py:

and add to pipelines in settings.py:

SOCIAL_AUTH_PIPELINE += (
'<application>.pipelines.save_profile_picture',
)

这篇关于使用python-social-auth在模型中保存Facebook个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆