From: Andrew Korzhuev Date: Wed, 2 May 2012 22:07:48 +0000 (+0400) Subject: Throw exceptions if data-retrieval fails. Better comments X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=a0319a1f95efc3f49e00590d1df457690047ead0;p=django-social-auth.git Throw exceptions if data-retrieval fails. Better comments --- diff --git a/social_auth/backends/contrib/live.py b/social_auth/backends/contrib/live.py index c3680be..66a003f 100644 --- a/social_auth/backends/contrib/live.py +++ b/social_auth/backends/contrib/live.py @@ -4,12 +4,15 @@ MSN Live Connect oAuth 2.0 Settings: LIVE_CLIENT_ID LIVE_CLIENT_SECRET -LIVE_EXTENDED_PERMISSIONS (default: wl.basic, wl.emails) +LIVE_EXTENDED_PERMISSIONS (defaults are: wl.basic, wl.emails) References: * oAuth http://msdn.microsoft.com/en-us/library/live/hh243649.aspx * Scopes http://msdn.microsoft.com/en-us/library/live/hh243646.aspx * REST http://msdn.microsoft.com/en-us/library/live/hh243648.aspx + +Throws: +AuthUnknownError - if user data retrieval fails """ from urllib import urlencode, urlopen @@ -17,6 +20,7 @@ from django.utils import simplejson from social_auth.utils import setting from social_auth.backends import BaseOAuth2, OAuthBackend, USERNAME +from social_auth.backends.exceptions import AuthUnknownError # Live Connect configuration @@ -24,7 +28,7 @@ LIVE_AUTHORIZATION_URL = 'https://login.live.com/oauth20_authorize.srf' LIVE_ACCESS_TOKEN_URL = 'https://login.live.com/oauth20_token.srf' LIVE_USER_DATA_URL = 'https://apis.live.net/v5.0/me' LIVE_SERVER = 'live.com' -LIVE_EXTENDED_PERMISSIONS = ['wl.basic', 'wl.emails'] +LIVE_DEFAULT_PERMISSIONS = ['wl.basic', 'wl.emails'] class LiveBackend(OAuthBackend): @@ -67,7 +71,7 @@ class LiveAuth(BaseOAuth2): def get_scope(self): """Return list with needed access scope""" - return setting('LIVE_EXTENDED_PERMISSIONS', LIVE_EXTENDED_PERMISSIONS) + return LIVE_DEFAULT_PERMISSIONS + setting('LIVE_EXTENDED_PERMISSIONS', []) def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" @@ -76,8 +80,8 @@ class LiveAuth(BaseOAuth2): }) try: return simplejson.load(urlopen(url)) - except ValueError: - return None + except (ValueError, IOError): + raise AuthUnknownError("Error during profile retrieval, please, try again later") # Backend definition diff --git a/social_auth/backends/contrib/yahoo.py b/social_auth/backends/contrib/yahoo.py index e890574..9ac234b 100644 --- a/social_auth/backends/contrib/yahoo.py +++ b/social_auth/backends/contrib/yahoo.py @@ -9,12 +9,20 @@ References: * http://developer.yahoo.com/oauth/guide/oauth-auth-flow.html * http://developer.yahoo.com/social/rest_api_guide/introspective-guid-resource.html * http://developer.yahoo.com/social/rest_api_guide/extended-profile-resource.html + +Scopes: +To make this extension works correctly you have to have at least +Yahoo Profile scope with Read permission + +Throws: +AuthUnknownError - if user data retrieval fails (guid or profile) """ from django.utils import simplejson from social_auth.utils import setting from social_auth.backends import ConsumerBasedOAuth, OAuthBackend, USERNAME +from social_auth.backends.exceptions import AuthUnknownError # Google OAuth base configuration @@ -38,7 +46,7 @@ class YahooOAuthBackend(OAuthBackend): return response['guid'] def get_user_details(self, response): - """Return user details from Orkut account""" + """Return user details from Yahoo Profile""" fname = response.get('givenName') lname = response.get('familyName') return {USERNAME: response.get('nickname'), @@ -66,9 +74,13 @@ class YahooOAuth(ConsumerBasedOAuth): try: return simplejson.loads(response)['profile'] except ValueError: - return None + raise AuthUnknownError("Error during profile retrieval, please, try again later") def _get_guid(self, access_token): + """ + Beause you have to provide GUID for every API request + it's also returned during one of OAuth calls + """ url = 'http://social.yahooapis.com/v1/me/guid?format=json' request = self.oauth_request(access_token, url) response = self.fetch_response(request) @@ -76,7 +88,7 @@ class YahooOAuth(ConsumerBasedOAuth): json = simplejson.loads(response) return json['guid']['value'] except ValueError: - return 'me' + raise AuthUnknownError("Error during user id retrieval, please, try again later") # Backend definition BACKENDS = {