我在我的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?
答案 0 :(得分:39)
python-social-auth
是django-social-auth
的较新派生版本。
AlexYar的answer可以通过修改python-social-auth
稍加修改以与settings.py
一起使用,并进行以下更改:
添加中间件以处理SocialAuthException
MIDDLEWARE_CLASSES += (
'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
)
发生异常时重定向到的网址
SOCIAL_AUTH_LOGIN_ERROR_URL = '/'
请注意,您还需要设置
DEBUG = False
全部或阅读http://python-social-auth.readthedocs.org/en/latest/configuration/django.html#exceptions-middleware
答案 1 :(得分:21)
你可以创建一个中间件并捕获任何异常, 例外列表:https://github.com/omab/python-social-auth/blob/master/social/exceptions.py 在这种情况下,您的AuthCanceled Exception。
<强> middleware.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
<强> settings.py 强>
MIDDLEWARE_CLASSES = (
.....
'pat_to_middleware.SocialAuthExceptionMiddleware',
)
答案 2 :(得分:18)
这是对@Nicolas答案的略微修改,这对我有用。
<强> middleware.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
<强> settings.py 强>
MIDDLEWARE_CLASSES += (
'myapp.middleware.SocialAuthExceptionMiddleware',
)
答案 3 :(得分:2)
只需加入
MIDDLEWARE_CLASSES =( 'social_auth.middleware.SocialAuthExceptionMiddleware', )
等等
LOGIN_ERROR_URL ='/'
这就是全部 或阅读http://django-social-auth.readthedocs.org/en/latest/configuration.html#exceptions-middleware
答案 4 :(得分:1)
2018年答案:
将SocialAuthExceptionMiddleware
中间件添加到您的配置中:
MIDDLEWARE_CLASSES = [
...
'social_django.middleware.SocialAuthExceptionMiddleware',
]
在您的配置中设置SOCIAL_AUTH_LOGIN_ERROR_URL
:
SOCIAL_AUTH_LOGIN_ERROR_URL = '/login'
现在,当您拥有DEBUG = False
时,您的用户将在社交身份验证提供程序页面中点击取消时被重定向到您的登录页面。
DEBUG = True
在开发过程中,您仍会在浏览器中看到错误页面。
答案 5 :(得分:0)
如果您不关心处理异常,请在settings.py
SOCIAL_AUTH_RAISE_EXCEPTIONS = False
答案 6 :(得分: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