19

django-social-auth某些情况下,后端会引发 a ValueError(例如,当用户取消登录请求或用户尝试与已与另一个用户关联的帐户关联时)。如果用户遇到这些情况之一,他们将在您的网站上看到 500 错误。

那么,捕捉这些的最佳方法是什么?发生这种情况时,我希望能够显示有用的消息(通过消息框架),但我不知道最好的方法。

我正在考虑编写我自己的视图(在一个单独的应用程序中),它只是包装了social_authassociate_complete视图,但这似乎很笨重......有什么想法吗?

我可以分叉django-social-auth和定制这种行为,但我不想维护一个单独的分叉——特别是因为我不能假设每个人都想以同样的方式处理这些异常。

4

4 回答 4

19

相当老的问题,但值得一提的是,最新版本的 DSA 支持自定义异常处理器,您可以在其中对异常消息执行任何操作。默认版本将它们存储在消息应用程序中。

此外,现在区分了异常,而不是使用了无用的异常ValueError。检查文档http://django-social-auth.readthedocs.org/en/latest/configuration.html

更新(2013 年 13 月 8 日):

由于我已经发布了上述内容已经改变,现在 DSA 有一个异常中间件,当启用时,它将异常消息存储在 jango 内置消息应用程序中。最好对中间件进行子类化并向其中添加自定义行为。在http://django-social-auth.readthedocs.org/en/latest/configuration.html#exceptions-middleware查看文档

示例中间件:

# -*- coding: utf-8 -*-
from social_auth.middleware import SocialAuthExceptionMiddleware
from social_auth.exceptions import AuthFailed
from django.contrib import messages

class CustomSocialAuthExceptionMiddleware( SocialAuthExceptionMiddleware):

    def get_message(self, request, exception):
        msg = None
        if (isinstance(exception, AuthFailed) and 
            exception.message == u"User not allowed"):
            msg =   u"Not in whitelist" 
        else:
            msg =   u"Some other problem"    
        messages.add_message(request, messages.ERROR, msg)     
于 2012-03-03T01:41:27.900 回答
13

我遇到了同样的问题,至少在这一点上,创建包装视图是处理这种情况的最佳方法。这是我的完成方式:

def social_auth_login(request, backend):
    """
        This view is a wrapper to social_auths auth
        It is required, because social_auth just throws ValueError and gets user to 500 error
        after every unexpected action. This view handles exceptions in human friendly way.
        See https://convore.com/django-social-auth/best-way-to-handle-exceptions/
    """
    from social_auth.views import auth

    try:
        # if everything is ok, then original view gets returned, no problem
        return auth(request, backend)
    except ValueError, error:
        # in case of errors, let's show a special page that will explain what happened
        return render_to_response('users/login_error.html',
                                  locals(),
                                  context_instance=RequestContext(request))

您必须为此进行设置url

urlpatterns = patterns('',
    # ...
    url(r'^social_auth_login/([a-z]+)$',  social_auth_login, name='users-social-auth-login'), 
)

然后像以前一样在模板中使用它:

<a href="{% url 'users-social-auth-login' "google" %}">Log in with Google</a>

希望这会有所帮助,即使是在提出问题两个月后:)

于 2011-08-24T08:26:50.010 回答
6

您需要添加社交身份验证中间件:

MIDDLEWARE_CLASSES += ('social_auth.middleware.SocialAuthExceptionMiddleware',)

如果发生任何错误,用户将被重定向到错误 url(来自设置的 LOGIN_ERROR_URL)。

有关详细说明,请参阅文档: http ://django-social-auth.readthedocs.org/en/latest/configuration.html#exceptions-middleware

于 2013-08-28T07:50:27.530 回答
5

在我的应用程序的views.py中:

from social_auth.views import associate_complete

def associate_complete_wrapper(request, backend):
    try:
        return associate_complete(request, backend)
    except ValueError, e:
        if e and len(e.args) > 0:
            messages.error(request, "Failed to Associate: %s" % e.args[0])
    return redirect(reverse('pieprofile-edit-account'))

然后在 Root URLconf 中(注意这些 url 模式的顺序):

url(r'^associate/complete/(?P<backend>[^/]+)/$', 'myapp.views.associate_complete_wrapper'),
url(r'', include('social_auth.urls')),

associate_complete_wrapper的 url 本质上劫持了 social_auth 的socialauth_associate_completeurl。

于 2011-08-26T02:50:58.287 回答