Using extra_data from python-social-auth in Django template
Asked Answered
H

1

6

I am using python-social-auth to log in my own web application from LinkedIn. I successfully logged in and redirect to my home page. The next step is to utilize the extra data (i.e. UserSocialAuth.extra_data). Could anyone give a example about how to access the extra data in either Django templates or Django views?

This is the settings I had.

SOCIAL_AUTH_LOGIN_URL = ''

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'
)

SOCIAL_AUTH_LINKEDIN_OAUTH2_SCOPE = ['r_basicprofile', 'r_emailaddress', 'rw_company_admin', 'w_share']
SOCIAL_AUTH_LINKEDIN_OAUTH2_FIELD_SELECTORS = ['first-name', 'last-name', 'email-address', 'headline']

SOCIAL_AUTH_LINKEDIN_OAUTH2_EXTRA_DATA = [('id', 'id'),
                                      ('first-name', 'first_name'),
                                      ('last-name', 'last_name'),
                                      ('email-address', 'email_address'),
                                      ('headline', 'headline'),
                                      ('industry', 'industry')]

What I am going to do is like:

<html><body>Here we go! {{ user.first_name }} {{ user.last_name }}
{{ user.email }}
{{ user.extra_data.headline }}</body></html>

Any comments will be highly appreciated!

Hoff answered 5/11, 2016 at 15:30 Comment(0)
R
5

You can access the data in your Django view like so:

if request.user.is_authenticated:
    linkedin_user = request.user.social_auth.get(provider ='linkedin-oauth2')
    extra_data = linkedin_user.extra_data

And you can then inject the data in your template to be displayed

Rajput answered 10/2, 2018 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.