首页 > 解决方案 > TypeError:Django 社交登录中的格式字符串参数不足

问题描述

我正在使用 social-auth-app-django 进行 Google 身份验证。每当我尝试使用 google 登录或管理页面时,都会遇到此错误。 在此处输入图像描述

我已在 settings.py 、视图和模板中正确添加了所有内容。

Traceback (most recent call last):
  File "C:\Data Science\python_files\temop\lib\site-packages\social_core\backends\utils.py", line 50, in get_backend
    return BACKENDSCACHE[name]
KeyError: 'google-oauth2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Santosh Chirag\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openid\oidutil.py", line 112, in importElementTree
    ElementTree.XML('<unused/>')
  File "C:\Data Science\python_files\temop\lib\site-packages\defusedxml\common.py", line 113, in fromstring
    parser = DefusedXMLParser(target=_TreeBuilder(),
  File "C:\Data Science\python_files\temop\lib\site-packages\defusedxml\ElementTree.py", line 68, in __init__
    _XMLParser.__init__(self, html, target, encoding)
TypeError: __init__() takes 1 positional argument but 4 were given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Data Science\python_files\temop\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Data Science\python_files\temop\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Data Science\python_files\temop\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Data Science\python_files\temop\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Data Science\python_files\temop\lib\site-packages\social_django\utils.py", line 45, in wrapper
    request.backend = load_backend(request.social_strategy,
  File "C:\Data Science\python_files\temop\lib\site-packages\social_django\utils.py", line 27, in load_backend
    Backend = get_backend(BACKENDS, name)
  File "C:\Data Science\python_files\temop\lib\site-packages\social_core\backends\utils.py", line 53, in get_backend
    load_backends(backends, force_load=True)
  File "C:\Data Science\python_files\temop\lib\site-packages\social_core\backends\utils.py", line 35, in load_backends
    backend = module_member(auth_backend)
  File "C:\Data Science\python_files\temop\lib\site-packages\social_core\utils.py", line 61, in module_member
    module = import_module(mod)
  File "C:\Data Science\python_files\temop\lib\site-packages\social_core\utils.py", line 55, in import_module
    __import__(name)
  File "C:\Data Science\python_files\temop\lib\site-packages\social_core\backends\google.py", line 6, in <module>
    from .open_id import OpenIdAuth
  File "C:\Data Science\python_files\temop\lib\site-packages\social_core\backends\open_id.py", line 6, in <module>
    from openid.consumer.consumer import Consumer, SUCCESS, CANCEL, FAILURE
  File "C:\Users\Santosh Chirag\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openid\consumer\consumer.py", line 196, in <module>
    from openid.consumer.discover import discover, OpenIDServiceEndpoint, \
  File "C:\Users\Santosh Chirag\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openid\consumer\discover.py", line 22, in <module>
    from openid.yadis.etxrd import nsTag, XRDSError, XRD_NS_2_0
  File "C:\Users\Santosh Chirag\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openid\yadis\etxrd.py", line 31, in <module>
    SafeElementTree = importSafeElementTree()
  File "C:\Users\Santosh Chirag\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openid\oidutil.py", line 83, in importSafeElementTree
    return importElementTree(module_names)
  File "C:\Users\Santosh Chirag\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openid\oidutil.py", line 117, in importElementTree
    'Not using ElementTree library %r because it failed to '
    TypeError: not enough arguments for format string

该错误似乎与 social-auth-app-django 模块有关。

SOCIAL_AUTH_URL_NAMESPACE = 'social'#
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'SOCIAL_GOOGLE_KEY'  #Paste CLient Key
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'SECRET KEY'
AUTHENTICATION_BACKENDS = (
    'social_core.backends.google.GoogleOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

网址.py

path('', include('social_django.urls', namespace='social')),

登录.html

    <a href="{% url 'social:begin' 'google-oauth2' %}" class="google btn"><i class="fa fa-google fa-fw">

标签: pythondjangoformsgoogle-signin

解决方案


我遇到了这个问题,并通过在我的虚拟环境中切换 python 版本来解决它。

这是由于一个 django 依赖项和 python 版本之间的冲突。错误发生在python 3.8django 2 上的虚拟环境中。在 中创建新的虚拟环境python 3.6并重新安装所有要求修复了它。


推荐阅读