Search code examples
djangopython-social-authdjango-socialauth

Django linkedin authentication programming error


When I try to authenticate with linkedin in my django app, it passes the first stage where it asks me to give permission to linkedin to access my data, but fails to authenticate after that and displays this error.

Django Debug page

I have searched through the net and I have not seen anything on WSGIRequest. This is my Linkedin authentication profile in my settings file

SOCIAL_AUTH_LINKEDIN_OAUTH2_KEY = '7756p783kegdt2'
SOCIAL_AUTH_LINKEDIN_OAUTH2_SECRET = 'irDH1gCxSJKICfhi'
SOCIAL_AUTH_LINKEDIN_OAUTH2_SCOPE = ['r_basicprofile', 'r_emailaddress']
SOCIAL_AUTH_LINKEDIN_OAUTH2_FIELD_SELECTORS = [
'email-address', 'headline', 'industry']
SOCIAL_AUTH_LINKEDIN_OAUTH2_EXTRA_DATA = [
('id', 'id'),
('first-name', 'first_name'),
('last-name', 'last_name'),
('email-address', 'email_address'),
('industry', 'industry'),
]

SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.social_auth.associate_by_email',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
)

This is my login urls being called in my settings file

LOGIN_URL = '/#authenticate'
LOGOUT_URL = '/user/logout'
LOGIN_REDIRECT_URL = '/accounts/'
LOGOUT_REDIRECT_URL = '/'

This is my view handling the user authentication

class CaseInsensitiveModelBackend(ModelBackend):
""" Allows Case insensitive Authentication for Username/emails """

    def authenticate(self, username=None, password=None, **kwargs):
    """ confirm validity of username and password"""

    try:
        user = User.objects.get(username__iexact=username)
        if user.check_password(password):
            return user
    except User.DoesNotExist:
        return None

    def get_user(self, user_id):
    try:
        return User.objects.get(pk=user_id)
    except User.DoesNotExist:
        return None

This are my important urls

path(r'accounts/register', views.register, name='register'),
path(
    r'accounts/account-activation-sent/', views.account_activation_sent,
    name='account_activation_sent'
),
path(
    r'accounts/activate/<uidb64>/<token>/', views.activate,
    name='activate'
),
path(r'purchase/<token>/', views.purchase, name='purchase'),
path(r'<str:username>/deactivate/', views.deactivate, name='deactivate'),
path(r'deactivated/', views.deactivated, name='deactivated'),
path(r'accounts/login/', views.user_login, name='login'),

This is the url I want the user to be redirected to after authentication with linkedin

path(r'<str:username>/', views.dashboard, name='dashboard'),

Solution

  • The first argument to authenticate is the request, not the username - see the docs. You need to preserve that signature.

    class CaseInsensitiveModelBackend(ModelBackend):
        def authenticate(self, request, username=None, password=None, **kwargs):