13

我使用django-rest-authdjango-allauth关于使用用户的 Facebook 个人资料进行用户注册/登录。

现在,我可以从 Facebook 获取一些标准信息(所有身份验证的默认设置)。

是否可以检索头像图片列表?如何在 allAuth 中实现它?

4

3 回答 3

24

如果要在 django 模板中获取头像图片:

<img src="{{ user.socialaccount_set.all.0.get_avatar_url }}" />

那是你想要做的吗?

于 2015-04-11T01:39:16.740 回答
11

让我快速描述一下我是如何做到的(使用 all_auth 和 rest_auth)。

基本上,'allauth => socialaccount'提供user_signed_upuser_logged_in信号。因此,您需要捕获它们并获取 sociallogin 对象的额外数据并填充您的 avatar_url。

Step-1:创建一个UserProfile模型:(存储avatar_url)

#models.py
try:
    from django.utils.encoding import force_text
except ImportError:
    from django.utils.encoding import force_unicode as force_text

class UserProfile(models.Model):
    user = models.OneToOneField(User, primary_key=True, verbose_name='user', related_name='profile')
    avatar_url = models.CharField(max_length=256, blank=True, null=True)

    def __str__(self):
        return force_text(self.user.email)

    class Meta():
        db_table = 'user_profile'

第 2 步:捕获 'user_signed_up' 信号并填充 'avatar_url'

# signals.py

from allauth.account.signals import user_signed_up, user_logged_in

@receiver(user_signed_up)
def social_login_fname_lname_profilepic(sociallogin, user):
    preferred_avatar_size_pixels=256

    picture_url = "http://www.gravatar.com/avatar/{0}?s={1}".format(
        hashlib.md5(user.email.encode('UTF-8')).hexdigest(),
        preferred_avatar_size_pixels
    )

    if sociallogin:
        # Extract first / last names from social nets and store on User record
        if sociallogin.account.provider == 'twitter':
            name = sociallogin.account.extra_data['name']
            user.first_name = name.split()[0]
            user.last_name = name.split()[1]

        if sociallogin.account.provider == 'facebook':
            f_name = sociallogin.account.extra_data['first_name']
            l_name = sociallogin.account.extra_data['last_name']
            if f_name:
                user.first_name = f_name
            if l_name:
                user.last_name = l_name

            #verified = sociallogin.account.extra_data['verified']
            picture_url = "http://graph.facebook.com/{0}/picture?width={1}&height={1}".format(
                sociallogin.account.uid, preferred_avatar_size_pixels)

        if sociallogin.account.provider == 'google':
            f_name = sociallogin.account.extra_data['given_name']
            l_name = sociallogin.account.extra_data['family_name']
            if f_name:
                user.first_name = f_name
            if l_name:
                user.last_name = l_name
            #verified = sociallogin.account.extra_data['verified_email']
            picture_url = sociallogin.account.extra_data['picture']

    user.save()
    profile = UserProfile(user=user, avatar_url=picture_url)
    profile.save()        

希望这会帮助你。

于 2016-11-09T01:10:00.183 回答
0

如果您不想使用 Django 信号,您可以扩展DefaultSocialAccountAdapterallauth.socialaccount.adapter覆盖populate_user

from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.account.utils import user_field


class CustomSocialAccountAdapter(DefaultSocialAccountAdapter):
    def populate_user(self, request, sociallogin, data):
        user = super().populate_user(request, sociallogin, data)
        try:
            picture = sociallogin.account.extra_data['picture']
            user_field(user, "profile_photo", picture)
        except (KeyError, AttributeError):
            pass
        return user

DefaultSocialAccountAdapter请注意,您还应该更改settings.py

SOCIALACCOUNT_ADAPTER = 'some_app.path.to.CustomSocialAccountAdapter'
于 2021-02-10T00:05:46.763 回答