How to populate user profile with django-allauth provider information?

前端 未结 4 457
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 02:36

I\'m using django-allauth for my authentication system. I need that when the user sign in, the profile module get populated with the provider info (in my case facebook).

相关标签:
4条回答
  • 2020-12-13 03:17

    There is an easier way to do this.

    Just add the following to your settings.py. For example, Linked in...

    SOCIALACCOUNT_PROVIDERS = {
    'linkedin': {
        'SCOPE': [
            'r_basicprofile',
            'r_emailaddress'
        ],
        'PROFILE_FIELDS': [
            'id',
            'first-name',
            'last-name',
            'email-address',
            'picture-url',
            'public-profile-url',
        ]
    }
    

    The fields are automatically pulled across.

    0 讨论(0)
  • 2020-12-13 03:18

    The pre_social_login signal is sent after a user successfully authenticates via a social provider, but before the login is actually processed. This signal is emitted for social logins, signups and when connecting additional social accounts to an account.

    So it is sent before the signup is fully completed -- therefore this not the proper signal to use.

    Instead, I recommend you use allauth.account.signals.user_signed_up, which is emitted for all users, local and social ones.

    From within that handler you can inspect whatever SocialAccount is attached to the user. For example, if you want to inspect Google+ specific data, do this:

    user.socialaccount_set.filter(provider='google')[0].extra_data
    

    UPDATE: the latest development version makes this a little bit more convenient by passing along a sociallogin parameter that directly contains all related info (social account, token, ...)

    0 讨论(0)
  • 2020-12-13 03:20

    I am doing in this way and taking picture (field) url and google provider(field) as an example.

    socialaccount_obj = SocialAccount.objects.filter(provider='google', user_id=self.user.id)
       picture = "not available"
       if len(socialaccount_obj):
                picture = socialaccount_obj[0].extra_data['picture']
    

    make sure to import : from allauth.socialaccount.models import SocialAccount

    0 讨论(0)
  • 2020-12-13 03:32

    Here is a Concrete example of @pennersr solution :

    Assumming your profile model has these 3 fields: first_name, email, picture_url

    views.py:

    @receiver(user_signed_up)
    def populate_profile(sociallogin, user, **kwargs):    
    
        if sociallogin.account.provider == 'facebook':
            user_data = user.socialaccount_set.filter(provider='facebook')[0].extra_data
            picture_url = "http://graph.facebook.com/" + sociallogin.account.uid + "/picture?type=large"            
            email = user_data['email']
            first_name = user_data['first_name']
    
        if sociallogin.account.provider == 'linkedin':
            user_data = user.socialaccount_set.filter(provider='linkedin')[0].extra_data        
            picture_url = user_data['picture-urls']['picture-url']
            email = user_data['email-address']
            first_name = user_data['first-name']
    
        if sociallogin.account.provider == 'twitter':
            user_data = user.socialaccount_set.filter(provider='twitter')[0].extra_data
            picture_url = user_data['profile_image_url']
            picture_url = picture_url.rsplit("_", 1)[0] + "." + picture_url.rsplit(".", 1)[1]
            email = user_data['email']
            first_name = user_data['name'].split()[0]
    
        user.profile.avatar_url = picture_url
        user.profile.email_address = email
        user.profile.first_name = first_name
        user.profile.save()         
    

    If you are confused about those picture_url variable in each provider. Then take a look at the docs:

    facebook:

    picture_url = "http://graph.facebook.com/" + sociallogin.account.uid + "/picture?type=large" Docs

    linkedin:

    picture_url = user_data['picture-urls']['picture-url'] Docs

    twitter:

    picture_url = picture_url.rsplit("_", 1)[0] + "." + picture_url.rsplit(".", 1)[1] Docs And for the rsplit() take a look here

    Hope that helps. :)

    0 讨论(0)
提交回复
热议问题