我使用django-rest-auth
和django-allauth
关于使用用户的 Facebook 个人资料进行用户注册/登录。
现在,我可以从 Facebook 获取一些标准信息(所有身份验证的默认设置)。
是否可以检索头像图片列表?如何在 allAuth 中实现它?
我使用django-rest-auth
和django-allauth
关于使用用户的 Facebook 个人资料进行用户注册/登录。
现在,我可以从 Facebook 获取一些标准信息(所有身份验证的默认设置)。
是否可以检索头像图片列表?如何在 allAuth 中实现它?
如果要在 django 模板中获取头像图片:
<img src="{{ user.socialaccount_set.all.0.get_avatar_url }}" />
那是你想要做的吗?
让我快速描述一下我是如何做到的(使用 all_auth 和 rest_auth)。
基本上,'allauth => socialaccount'提供user_signed_up 和user_logged_in信号。因此,您需要捕获它们并获取 sociallogin 对象的额外数据并填充您的 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'
# 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()
希望这会帮助你。
如果您不想使用 Django 信号,您可以扩展DefaultSocialAccountAdapter
并allauth.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'