如何在通过 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/