python - 使用 python-social-auth 在模型中保存 facebook 个人资料图片

标签 python django facebook django-socialauth

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

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

知道如何使用 python-social-auth 和管道做同样的事情吗?

最佳答案

这就是我的工作方式。 (来自https://github.com/omab/python-social-auth/issues/80)

在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 中的管道:

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

关于python - 使用 python-social-auth 在模型中保存 facebook 个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19890824/

相关文章:

python - 登录 Facebook 后如何重定向 url?

python - 全文搜索的各种搜索算法和性能

python - 从 django 查询集计算百分位数

python - 如何防止在重新加载 cog 后出现 AttributeError?

python - Sockjs - 在 Python 代码中向 sockjs-tornado 发送消息

OSX 10.5 上的 Python 2.7 Virtualenv 路径问题

javascript - Facebook - 如何获取个人资料图片

ios - FB.login()phonegap facebook 插件出错

python - BeautifulSoup - 如何从 html 类中获取所有 <div> ?

python - 为键 : value from two dimension list 创建字典