30

我在我的 Django 应用程序中使用 python-social-auth 通过 Facebook 进行身份验证。但是当用户尝试登录时,他们已经被重定向到 Facebook 应用页面,当他们点击“取消”按钮时,会出现以下异常:

ERROR 2014-01-03 15:32:15,308 base :: Internal Server Error: /complete/facebook/
Traceback (most recent call last):
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 45, in wrapper
    return func(request, backend, *args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/apps/django_app/views.py", line 21, in complete
    redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/actions.py", line 54, in do_complete
    *args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/strategies/base.py", line 62, in complete
    return self.backend.auth_complete(*args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/facebook.py", line 63, in auth_complete
    self.process_error(self.data)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/facebook.py", line 56, in process_error
    super(FacebookOAuth2, self).process_error(data)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/oauth.py", line 312, in process_error
    raise AuthCanceled(self, data.get('error_description', ''))
AuthCanceled: Authentication process canceled

有什么方法可以抓住它 Django 吗?

4

7 回答 7

45

python-social-authdjango-social-auth.

AlexYar 的答案可以python-social-auth通过settings.py以下更改进行稍微修改以使用:

  1. 添加一个中间件来处理 SocialAuthException

    MIDDLEWARE_CLASSES += (
        'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
    )
    
  2. 发生异常时重定向到的 URL

    SOCIAL_AUTH_LOGIN_ERROR_URL = '/'
    
  3. 请注意,您还需要设置

    DEBUG = False
    

这就是全部或阅读http://python-social-auth.readthedocs.org/en/latest/configuration/django.html#exceptions-middleware

于 2014-08-19T16:11:52.230 回答
21

您可以创建一个中间件并捕获任何异常,异常列表:https ://github.com/omab/python-social-auth/blob/master/social/exceptions.py 在这种情况下是您的 AuthCanceled 异常。

中间件.py



from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from django.shortcuts import HttpResponse
from social import exceptions as social_exceptions

class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if hasattr(social_exceptions, 'AuthCanceled'):
            return HttpResponse("I'm the Pony %s" % exception)
        else:
            raise exception


设置.py



MIDDLEWARE_CLASSES = (
        .....
        'pat_to_middleware.SocialAuthExceptionMiddleware',
)


于 2014-01-06T20:26:50.557 回答
18

这是对@Nicolas 答案的轻微修改,这对我有用。

中间件.py

from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from django.shortcuts import render
from social.exceptions import AuthCanceled

class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if type(exception) == AuthCanceled:
            return render(request, "pysocial/authcancelled.html", {})
        else:
            pass

设置.py

MIDDLEWARE_CLASSES += (
'myapp.middleware.SocialAuthExceptionMiddleware',
)
于 2014-02-20T06:28:24.720 回答
3

2018年的答案:

  1. SocialAuthExceptionMiddleware中间件添加到您的配置中:

    MIDDLEWARE_CLASSES = [
        ...
        'social_django.middleware.SocialAuthExceptionMiddleware',
    ]
    
  2. 在您的配置中设置SOCIAL_AUTH_LOGIN_ERROR_URL

    SOCIAL_AUTH_LOGIN_ERROR_URL = '/login'
    

现在,当您拥有 时DEBUG = False,当您的用户在社交身份验证提供商页面中单击取消时,他们将被重定向到您的登录页面。

DEBUG = True您在开发过程中仍会在浏览器中看到错误页面时。

于 2018-01-29T14:12:14.093 回答
2

只需添加

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

和类似的东西

LOGIN_ERROR_URL = '/'

这就是全部或阅读http://django-social-auth.readthedocs.org/en/latest/configuration.html#exceptions-middleware

于 2014-02-27T05:36:14.373 回答
1

如果您不关心处理异常,请在您的settings.py

SOCIAL_AUTH_RAISE_EXCEPTIONS = False

看到这个答案:如何解决身份验证过程取消错误?

于 2016-11-15T21:14:54.210 回答
0

这是使用 social_django 更新的导入中间件

from social_django.middleware import SocialAuthExceptionMiddleware
from social_core import exceptions as social_exceptions
from django.shortcuts import HttpResponse
from django.shortcuts import render, redirect

class FacebookAuthCanceledExceptionMiddleware(SocialAuthExceptionMiddleware):

    def process_exception(self, request, exception):
        if hasattr(social_exceptions, 'AuthCanceled'):
            return redirect('auth_login')
        else:
            raise exception
于 2017-11-30T14:44:54.410 回答