从python-social-auth获取django用户个人资料

时间:2015-05-22 03:27:29

标签: python django python-social-auth

我的Django项目允许用户使用python-social-auth的django集成使用Google或Facebook个人资料登录。

一旦他们登录,有没有办法在Google或Facebook上获取他们个人资料的链接?我试图做这样的事情:

from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from django.core.mail import mail_admins

@receiver(post_save, sender=User)
def alert_new_user(sender, **kwargs):
    instance = kwargs['instance']
    social = instance.social_auth.last() # Assuming .last() gives me the one just added
    if social is not None:
        mail_admins('New user resistration',
            'A new user registered with name {}'.format(instance.get_full_name()),
            '<html><head/><body>A new user, <a href="{}">{}</a>, registered.</body></html>'.format(
                ???What goes here???, instance.get_full_name()
            )
        )

1 个答案:

答案 0 :(得分:0)

您可以在登录前获取其个人资料的链接。

pipeline中,有一个名为social_details的函数可从指定的社交网络获取有关用户的信息。

如果我们看看那个方法,那很简单:

def social_details(backend, details, response, *args, **kwargs):
    return {'details': dict(backend.get_user_details(response),
                            **details)}

如果您print response

def social_details(backend, details, response, *args, **kwargs):
    print(response)
    return {'details': dict(backend.get_user_details(response),
                            **details)}

如果登录是通过Facbook,您将看到有一个参数link。现在,您可以获得link并保存/使用它。