【问题标题】:How do I handle exceptions on Python Social Auth [closed]如何处理 Python Social Auth 上的异常 [关闭]
【发布时间】:2013-10-18 19:35:36
【问题描述】:

如何处理Python Social Auth上的这种异常AuthAlreadyAssociated

我找到的所有答案都是针对Django Social Auth 的,但自从它们被写出来之后,似乎发生了很多变化。

【问题讨论】:

    标签: python django authentication social


    【解决方案1】:

    您可以在应用的middleware.py 中创建一个新的中间件:

    from social_django.middleware import SocialAuthExceptionMiddleware
    from social_core import exceptions as social_exceptions     
    from django.http import HttpResponse
    
    class MySocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
        def process_exception(self, request, exception):
            if hasattr(social_exceptions, exception.__class__.__name__):
                # Here you can handle the exception as you wish
                return HttpResponse("Exception %s while processing your social account." % exception)
            else:
                return super(MySocialAuthExceptionMiddleware, self).process_exception(request, exception)
    

    并将其路径添加到settings.py

    MIDDLEWARE_CLASSES = (
        ...
        'path.to.MySocialAuthExceptionMiddleware',
       )
    

    【讨论】:

    • 如果假设问题在某个地方,但它会在这里提出..
    • 如果要显式处理 AuthAlreadyAssociated 异常,请使用以下示例。 if isinstance(exception, social_exceptions.AuthAlreadyAssociated):。并重定向例如:return redirect(reverse('index')).
    猜你喜欢
    • 1970-01-01
    • 2014-01-21
    • 2018-04-18
    • 2017-11-11
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    相关资源
    最近更新 更多