Merge pull request #187 from marsam/master

Capture OAuthError in TwitterLoginSerializer
This commit is contained in:
mariodev 2016-04-14 19:08:18 +02:00
commit 3189a5c7a0
2 changed files with 7 additions and 13 deletions

View File

@ -15,12 +15,8 @@ from rest_framework import serializers
from requests.exceptions import HTTPError from requests.exceptions import HTTPError
# Import is needed only if we are using social login, in which # Import is needed only if we are using social login, in which
# case the allauth.socialaccount will be declared # case the allauth.socialaccount will be declared
if 'allauth.socialaccount' in settings.INSTALLED_APPS: if 'allauth.socialaccount' in settings.INSTALLED_APPS:
try: from allauth.socialaccount.helpers import complete_social_login
from allauth.socialaccount.helpers import complete_social_login
except ImportError:
pass
class SocialLoginSerializer(serializers.Serializer): class SocialLoginSerializer(serializers.Serializer):

View File

@ -1,14 +1,12 @@
from django.conf import settings
from django.http import HttpRequest from django.http import HttpRequest
from rest_framework import serializers from rest_framework import serializers
from requests.exceptions import HTTPError
# Import is needed only if we are using social login, in which # Import is needed only if we are using social login, in which
# case the allauth.socialaccount will be declared # case the allauth.socialaccount will be declared
try: if 'allauth.socialaccount' in settings.INSTALLED_APPS:
from allauth.socialaccount.helpers import complete_social_login from allauth.socialaccount.helpers import complete_social_login
except ImportError: from allauth.socialaccount.models import SocialToken
pass from allauth.socialaccount.providers.oauth.client import OAuthError
from allauth.socialaccount.models import SocialToken
class TwitterLoginSerializer(serializers.Serializer): class TwitterLoginSerializer(serializers.Serializer):
@ -67,8 +65,8 @@ class TwitterLoginSerializer(serializers.Serializer):
try: try:
login = self.get_social_login(adapter, app, token, access_token) login = self.get_social_login(adapter, app, token, access_token)
complete_social_login(request, login) complete_social_login(request, login)
except HTTPError: except OAuthError as e:
raise serializers.ValidationError('Incorrect value') raise serializers.ValidationError(str(e))
if not login.is_existing: if not login.is_existing:
login.lookup() login.lookup()