Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using extra_data from python-social-auth in Django template

Tags:

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!

like image 849
Mouwu Lin Avatar asked Nov 05 '16 15:11

Mouwu Lin


1 Answers

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

like image 167
Thomas Graziani Avatar answered Sep 24 '22 14:09

Thomas Graziani