python-social-auth
python-social-auth copied to clipboard
Twitter backend occasionally fails
Internal Server Error: /login/twitter/
Internal Server Error: /login/twitter/
Traceback (most recent call last):
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 51, in wrapper
return func(request, backend, *args, **kwargs)
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/social/apps/django_app/views.py", line 19, in auth
return do_auth(request.backend, redirect_name=REDIRECT_FIELD_NAME)
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/social/actions.py", line 28, in do_auth
return backend.start()
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/social/backends/base.py", line 36, in start
return self.strategy.redirect(self.auth_url())
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/social/backends/oauth.py", line 165, in auth_url
token = self.set_unauthorized_token()
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/social/backends/oauth.py", line 221, in set_unauthorized_token
token = self.unauthorized_token()
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/social/backends/oauth.py", line 245, in unauthorized_token
method=self.REQUEST_TOKEN_METHOD
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/social/backends/base.py", line 225, in request
response.raise_for_status()
File "/home/paperc/.pyenv/versions/paperc/lib/python2.7/site-packages/requests/models.py", line 851, in raise_for_status
raise HTTPError(http_error_msg, response=self)
HTTPError: 503 Server Error: Service Temporarily Unavailable
Shouldn't this be caught and redirected to SOCIAL_AUTH_LOGIN_ERROR_URL instead of generating a 500?
You can create a custom middleware to catch these exceptions. Like this one:
from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from social.exceptions import AuthCanceled
from django.shortcuts import redirect, render
class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
"""
Handles login error exception generated by social authentication app
"""
def process_exception(self, request, exception):
if type(exception) == AuthCanceled:
return render(request, 'login_error.html', {'error_message': exception})
else:
raise exception
This one catches exceptions on cancelled Authentications. You can edit and create one that fits your needs.