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

标签 python django azure django-socialauth

我正在尝试使用 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)

设置.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',
)

关于python - 尝试将通过social_django验证的用户添加到组时出现错误 `set object is not subscriptable`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47938439/

相关文章:

azure - 将 key 从 Keyvault 同步到 Azure AKS 群集

Azure 容器应用程序无法从不同订阅中存在的 Azure 容器注册表中提取镜像

windows - 存储在 Windows Azure blob 上的视频不适用于 VideoJS(使用 Chrome)

python - Django 通用 View 中 get_context_data 和 queryset 之间的区别?

python - zappa 处理程序未导入 django 应用程序

python - 从 DataFrame 列中获取字符串并将其分配给 pandas 中的其他列

python - 是否有获取每组数据框中唯一值数量的函数?

python - 我正在尝试使用 dict 构建一个 sqlalchemy 引擎,该引擎具有从 tkinter 解析到它的变量

python - Tensorflow 张量 reshape 并用零填充

django - 有人知道好的 Django URL 命名空间教程吗?