0

更新- 按照我为 Twitch 尝试的相同步骤,我让 allauth 与谷歌合作。

首先,我对任何编程都很陌生。这是我在教程之外的第一个项目。

我正在尝试使用 django-allauth 使用 Twitch 登录。我正在使用 coockiecutter-django 开始我的项目,它会自动设置 django-allauth,但没有设置任何社交身份验证。我可以使用在我的网站上创建的帐户登录。

将“allauth.socialaccount.providers.twitch”添加到 INSTALLED_APPS 后,登录页面上会出现一个 Twitch 链接。我在http://www.twitch.tv/kraken/oauth2/clients/new注册了我的应用程序,并将所有内容复制到管理社交应用程序页面中。

单击 Twitch 登录链接后,我收到以下错误:

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'

我将尝试在我的项目代码中提供我认为相关的所有内容。如果我忘记了什么,请告诉我。

settings.py(在 coockiecutter-django 中重命名为 common.py)

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"

网址.py

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

Twitch 注册页面和我的管理页面中的所有信息都应该匹配。我将其全部复制/粘贴并检查了几次。删除所有信息并重新输入。

4

1 回答 1

2

您需要添加user_read范围,以便回调请求包含_id参数

将此添加到您的settings.py

 SOCIALACCOUNT_PROVIDERS = {
            "twitch": {"SCOPE": ["user_read"]},
    }
于 2014-11-08T10:09:45.610 回答