Django-allauth: KeyError at /accounts/linkedin_oauth2/login/callback/ 'id'

Created on 1 Aug 2018  路  12Comments  路  Source: pennersr/django-allauth

Hello,

I've been using django-allauth for a long time without any issues. Recently, I started getting strange errors.
There were absolutely no changes to the code done on our end, or upgrades, so I am wondering what might be causing this error.

The traceback is:

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic/hooks/framework_django.py" in wrapper
  544.                 return wrapped(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/allauth/socialaccount/providers/oauth2/views.py" in view
  73.                 return self.dispatch(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/allauth/socialaccount/providers/oauth2/views.py" in dispatch
  134.                                                 response=access_token)
File "/app/.heroku/python/lib/python2.7/site-packages/allauth/socialaccount/providers/linkedin_oauth2/views.py" in complete_login
  24.             request, extra_data)
File "/app/.heroku/python/lib/python2.7/site-packages/allauth/socialaccount/providers/base.py" in sociallogin_from_response
  86.         uid = self.extract_uid(response)
File "/app/.heroku/python/lib/python2.7/site-packages/allauth/socialaccount/providers/linkedin_oauth2/provider.py" in extract_uid
  37.         return str(data['id'])
Exception Type: KeyError at /accounts/linkedin_oauth2/login/callback/
Exception Value: 'id'

The versions are:

Django==1.11.12
django-allauth==0.35.0

This only happens with LinkedIN, not with Google. I myself was unable to reproduce the exact conditions when this happens. But I get a bunch of django error emails when users try to sign in, I guess.

Does anybody have a clue why this would suddenly be a problem and any idea how to fix it?

Most helpful comment

@apiljic I had the same problem. LinkedIn worked fine until a few days ago, so I thought this was most likely a recent change on their end.

I found that adding r_basicprofile to the scope fixed the problem.

settings.py:

SOCIALACCOUNT_PROVIDERS = {
    'linkedin_oauth2': {
        'SCOPE': [
            'r_emailaddress',
            'r_basicprofile',
        ],
    }
}

You should probably also add r_basicprofile to your LinkedIn app's Default Application Permissions.

All 12 comments

@apiljic I had the same problem. LinkedIn worked fine until a few days ago, so I thought this was most likely a recent change on their end.

I found that adding r_basicprofile to the scope fixed the problem.

settings.py:

SOCIALACCOUNT_PROVIDERS = {
    'linkedin_oauth2': {
        'SCOPE': [
            'r_emailaddress',
            'r_basicprofile',
        ],
    }
}

You should probably also add r_basicprofile to your LinkedIn app's Default Application Permissions.

@jjmontalbo Thank you very much for the tip! I just deployed the change you suggested. I tested it and there were no errors. I'll give it a few more hours to see if there are any errors when users register, but it looks like this fixes the problem.

For me, the fix that @jjmontalbo suggested did not work; however the difference for me is that I was specifying PROFILE_FIELDS in my SOCIALACCOUNT_PROVIDERS['linkedin_oauth2'] settings, and I needed to add id to this list:

SOCIALACCOUNT_PROVIDERS = {
    'linkedin_oauth2': {
        'SCOPE': [
            'r_emailaddress',
            'r_basicprofile',
        ],
        'PROFILE_FIELDS': [
            'id',
            'first-name',
            'last-name',
            'email-address',
        ],

@pydolan Glad to hear you fixed it too. The initial solution worked for me. I can confirm that. I guess now with two possible settings.py scenarios we can close this one.
Thanks again guys!

@apiljic -- can you re-open this, because allauth should be updated so that r_basicprofile is not required and id shouldn't need specifying in PROFILE_FIELDS? If id is needed, then users should not have the option to enable/disable it, etc. Thanks!

@pydolan OK.

I'm seeing similar behaviour on django-allauth==0.36.0. Similar, because I'm not seeing a KeyError, but the custom error page indicating that social login failed. Login with LinkedIn worked properly after adding the scopes and the fields to the settings

You should also add picture-url to PROFILE_FIELDS within the linkedin_oauth2 provider when you're using django-allauth==0.36.0 and you want to get the users' avatar. Quite possibly true for previous versions as well

Never mind this comment. It's working with the settings

was: Login with linkedin fails when using django-allauth==0.37.0, both with and without the settings.

Please upgrade to 0.37.1

Hi @pennersr ,

Currently using django-allauth 0.40. Login with Linkedin fails. I've added the above settings. using linkedin_oauth2. I get a code parameter in the callback url. Not able to identify what the exact error is. Could you please help?

In settings.py

SOCIALACCOUNT_QUERY_EMAIL = True

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

Hi @pennersr ,

Currently using django-allauth 0.40. Login with Linkedin fails. I've added the above settings. using linkedin_oauth2. I get a code parameter in the callback url. Not able to identify what the exact error is. Could you please help?

In settings.py

SOCIALACCOUNT_QUERY_EMAIL = True

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

you need to put 'r_liteprofile' instead of 'r_basicprofile'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

muhammedtufekyapan picture muhammedtufekyapan  路  5Comments

FSE-DEV picture FSE-DEV  路  5Comments

eillarra picture eillarra  路  3Comments

LukasKlement picture LukasKlement  路  4Comments

ConorMcGee picture ConorMcGee  路  5Comments