Search code examples
pythondjangoauthenticationdjango-allauthtwitch

Using django-allauth I am unable to log in with Twitch


UPDATE - I got allauth working with google following the same steps I tried for Twitch.

First off I'm quite new to programming anything. This is my first project outside of tutorials.

I'm trying to use django-allauth to log in using Twitch. I'm starting my project using coockiecutter-django that sets up django-allauth automatically, but without any social authentication set up. I can log in just fine with an account made on my site.

After adding 'allauth.socialaccount.providers.twitch', to the INSTALLED_APPS, a Twitch link appears on the log in page. I registered my app at http://www.twitch.tv/kraken/oauth2/clients/new and copied everything into the admin social application page.

After clicking on the Twitch log in link, I get the following error:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/twitch/login/callback/?code=8lhhsf461v7ksw0no3ajlwfslbdtia&scope=&state=xxKe31qCbow2

Django Version: 1.7.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'crispy_forms',
 'avatar',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.twitch',
 'users',
 'debug_toolbar')
Installed Middleware:
('djangosecure.middleware.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/home/brian/Envs/trycookie/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/brian/Envs/trycookie/local/lib/python2.7/site-packages/allauth/socialaccount/providers/oauth2/views.py" in view
  53.             return self.dispatch(request, *args, **kwargs)
File "/home/brian/Envs/trycookie/local/lib/python2.7/site-packages/allauth/socialaccount/providers/oauth2/views.py" in dispatch
  102.                                                 response=access_token)
File "/home/brian/Envs/trycookie/local/lib/python2.7/site-packages/allauth/socialaccount/providers/twitch/views.py" in complete_login
  21.                                                              extra_data)
File "/home/brian/Envs/trycookie/local/lib/python2.7/site-packages/allauth/socialaccount/providers/base.py" in sociallogin_from_response
  45.         uid = self.extract_uid(response)
File "/home/brian/Envs/trycookie/local/lib/python2.7/site-packages/allauth/socialaccount/providers/twitch/provider.py" in extract_uid
  25.         return str(data['_id'])

Exception Type: KeyError at /accounts/twitch/login/callback/
Exception Value: '_id'

I'll try to provide everything in my project code that I think is relevant. If I'm forgetting something please let me know.

settings.py (renamed common.py in coockiecutter-django)

INSTALLED_APPS = (
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.twitch',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
    "allauth.account.context_processors.account",
    "allauth.socialaccount.context_processors.socialaccount",
)

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)

SITE_ID = 1

ACCOUNT_AUTHENTICATION_METHOD = "username"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"

AUTH_USER_MODEL = "users.User"
LOGIN_REDIRECT_URL = "users:redirect"
LOGIN_URL = "account_login"

urls.py

url(r'^users/', include("users.urls", namespace="users")),
url(r'^accounts/', include('allauth.urls')),

All info in the Twitch registration page and my admin page should be matched up. I copy/pasted it all and checked it several times. Removing all info and entering it again.


Solution

  • You need to add the user_read scope so that the callback request contains the _id parameter

    Add this to your settings.py

     SOCIALACCOUNT_PROVIDERS = {
                "twitch": {"SCOPE": ["user_read"]},
        }