在 Django 模型中保存 Facebook 图片(REST Social Oauth2)

Saving Facebook picture in Django Model (REST Social Oath2)

这个问题是关于在 Django 模型中自动保存 Facebook 个人资料图片,使用 https://github.com/PhilipGarnero/django-rest-framework-social-oauth2 库。

编辑: 有 2 种方法可以解决此问题:将图像的 URL 保存在 CharField() 中或使用 ImageField() 保存图像本身。两种解决方案都可以。


上述库允许我使用不记名令牌创建和验证用户。我已经创建了配置文件模型:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='userprofile')
    photo = models.FileField(blank=True) # OR
    ######################################
    url = 'facebook.com{user id}/picture/'
    photo = models.CharField(default=url)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.userprofile.save()

自动为每个用户创建用户配置文件。现在,我想添加以下代码来保存来自 Facebook 的照片。 Facebook API requires user id 获取此图片。

photo = 'https://facebook/{user-id}/picture/'
UserProfile.objects.create(user=instance, photo=photo)

上面的方法不起作用,因为

1) 我不知道从哪里得到 user id

2) 图像不能这样存储,我需要将其转换为字节或其他一些方法。

有一个非常的简单解决方案。使用 python-social-auth pipelines.

这个东西的工作方式就像 middleware,你可以在你的设置页面的 SOCIAL_AUTH_PIPELINE 部分添加一个函数,每次用户通过身份验证时都会 运行 使用social_django.

一个例子:

在您的设置页面中,添加以下内容:

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',
    'home.pipeline.save_profile',
)

查看 home.pipeline.save_profile,这是 home.pipeline 文件中的新管道。 (改成自己的用户模块文件夹)

在那里(home.pipeline)添加以下内容:

from .models import UserProfile

def save_profile(backend, user, response, *args, **kwargs):
    if backend.name == "facebook":
        UserProfile.objects.create(
            user=user, 
            photo_url=response['user']['picture']
        )

这是一个例子。如果用户已经登录,您需要将其更改为 get/update。 另外,尝试使用 response 参数,您可能会在那里使用不同的数据。

最后一件事,确保将 picture 属性添加到您的设置中:

SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
  'fields': 'id, name, email, picture'
}

http://python-social-auth.readthedocs.io/en/latest/backends/facebook.html

https://godjango.com/122-custom-python-social-auth-pipeline/

https://github.com/python-social-auth/social-app-django

以上答案可能无效(对我无效),因为没有 accesstoken,facebook 个人资料 URL 将不再有效。以下答案对我有用。

def save_profile(backend, user, response, is_new=False, *args, **kwargs):
    if is_new and backend.name == "facebook":
        # The main part is how to get the profile picture URL and then do what you need to do
        Profile.objects.filter(owner=user).update(
            imageUrl='https://graph.facebook.com/{0}/picture/?type=large&access_token={1}'.format(response['id'],response['access_token']))

添加到 setting.py、

中的管道
SOCIAL_AUTH_PIPELINE+ = ('<full_path>.save_profile')