尝试将通过 social_django 验证的用户添加到组时出现错误“set object is not subscriptable”

Getting error `set object is not subscriptable` while trying to add user authenticated via social_django to a Group

我正在尝试将使用 social_django 通过 Microsoft Azure Active Directory 验证的用户添加到用户组。 这是我的 pipleline.py

from django.db.models import signals

from django.dispatch import Signal
from social.pipeline.user import *

from django.contrib.auth.models import User, Group
from social.utils import module_member

def new_users_handler(sender, user, response, details, **kwargs):
    user.groups.add(Group.objects.get(name='Customer'))

user_details.connect(new_users_handler, sender=None)

Settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'social_django',
    # 'social.apps.django_app.default',
]

AUTHENTICATION_BACKENDS = (
   'social.backends.azuread.AzureADOAuth2',
 )
#settings.py
SOCIAL_AUTH_PIPELINE = {
  'pipeline.new_users_handler'
}

这是完整的输出

TypeError at /complete/azuread-oauth2/
'set' object is not subscriptable
Request Method: GET
Request URL:    http://127.0.0.1:8000/complete/azuread-oauth2/?code=&state=&session_state=
Django Version: 1.11
Exception Type: TypeError
Exception Value:    
'set' object is not subscriptable
Exception Location: /home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/backends/base.py in run_pipeline, line 110
Python Executable:  /home/sudheer/self/venv/sso/bin/python
Python Version: 3.5.2
Python Path:    
['/home/sudheer/self/testsso',
 '/usr/lib/python35.zip',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-i386-linux-gnu',
 '/usr/lib/python3.5/lib-dynload',
 '/home/sudheer/self/venv/sso/lib/python3.5/site-packages']
Server time:    Fri, 22 Dec 2017 08:40:23 +00

我认为问题出在 self.run_pipeline(pipeline, pipeline_index, *args, **kwargs) 不执行自定义管道方法。 这是来自命令行的日志

Internal Server Error: /complete/azuread-oauth2/
Traceback (most recent call last):
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_django/utils.py", line 49, in wrapper
    return func(request, backend, *args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_django/views.py", line 33, in complete
    *args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/actions.py", line 41, in do_complete
    user = backend.complete(user=user, *args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/backends/base.py", line 40, in complete
    return self.auth_complete(*args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/utils.py", line 252, in wrapper
    return func(*args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/backends/oauth.py", line 399, in auth_complete
    *args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/utils.py", line 252, in wrapper
    return func(*args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/backends/oauth.py", line 410, in do_auth
    return self.strategy.authenticate(*args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_django/strategy.py", line 107, in authenticate
    return authenticate(*args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/django/contrib/auth/__init__.py", line 100, in authenticate
    user = backend.authenticate(*args, **credentials)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/backends/base.py", line 80, in authenticate
    return self.pipeline(pipeline, *args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/backends/base.py", line 83, in pipeline
    out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
  File "/home/sudheer/self/venv/sso/lib/python3.5/site-packages/social_core/backends/base.py", line 110, in run_pipeline
    for idx, name in enumerate(pipeline[pipeline_index:]):
TypeError: 'set' object is not subscriptable

您的 SOCIAL_AUTH_PIPELINE 设置应该是一个元组,而不是一个集合。你需要圆括号,而不是大括号,再加上一个逗号。

SOCIAL_AUTH_PIPELINE = (
  'pipeline.new_users_handler',
)