]> git.parisson.com Git - django-social-auth.git/commitdiff
add a function that gets name, given_name, family_name and add to user_details from...
authorKeiko Oda <keiko713@gmail.com>
Fri, 9 Mar 2012 08:41:59 +0000 (00:41 -0800)
committerKeiko Oda <keiko713@gmail.com>
Fri, 9 Mar 2012 08:41:59 +0000 (00:41 -0800)
social_auth/backends/google.py

index c596c4903d3174ec652904ba8ca9fdc32d48e248..142f7b1a5af2913b9af8d563eb07aba6ccdee985 100644 (file)
@@ -39,7 +39,10 @@ GOOGLE_OATUH2_AUTHORIZATION_URL = 'https://accounts.google.com/o/oauth2/auth'
 # scope for user email, specify extra scopes in settings, for example:
 # GOOGLE_OAUTH_EXTRA_SCOPE = ['https://www.google.com/m8/feeds/']
 GOOGLE_OAUTH_SCOPE = ['https://www.googleapis.com/auth/userinfo#email']
+GOOGLE_OAUTH2_SCOPE = ['https://www.googleapis.com/auth/userinfo.email', \
+                       'https://www.googleapis.com/auth/userinfo.profile']
 GOOGLEAPIS_EMAIL = 'https://www.googleapis.com/userinfo/email'
+GOOGLEAPIS_PROFILEE = 'https://www.googleapis.com/oauth2/v1/userinfo'
 GOOGLE_OPENID_URL = 'https://www.google.com/accounts/o8/id'
 
 
@@ -71,6 +74,14 @@ class GoogleOAuth2Backend(GoogleOAuthBackend):
         ('expires_in', setting('SOCIAL_AUTH_EXPIRATION', 'expires'))
     ]
 
+    def get_user_details(self, response):
+        email = response['email']
+        return {USERNAME: email.split('@', 1)[0],
+                'email': email,
+                'fullname': response.get('name', ''),
+                'first_name': response.get('given_name', ''),
+                'last_name': response.get('family_name', '')}
+
 
 class GoogleBackend(OpenIDBackend):
     """Google OpenID authentication backend"""
@@ -175,14 +186,13 @@ class GoogleOAuth2(BaseOAuth2):
     SETTINGS_SECRET_NAME = 'GOOGLE_OAUTH2_CLIENT_SECRET'
 
     def get_scope(self):
-        return GOOGLE_OAUTH_SCOPE + setting('GOOGLE_OAUTH_EXTRA_SCOPE', [])
+        return GOOGLE_OAUTH2_SCOPE + setting('GOOGLE_OAUTH_EXTRA_SCOPE', [])
 
     def user_data(self, access_token):
         """Return user data from Google API"""
-        data = {'oauth_token': access_token, 'alt': 'json'}
-        return googleapis_email(GOOGLEAPIS_EMAIL, urlencode(data))
-
+        return googleapis_profile(GOOGLEAPIS_PROFILEE, access_token)
 
 def googleapis_email(url, params):
     """Loads user data from googleapis service, only email so far as it's
     described in http://sites.google.com/site/oauthgoog/Home/emaildisplayscope
@@ -199,6 +209,19 @@ def googleapis_email(url, params):
         return None
 
 
+def googleapis_profile(url, access_token):
+    """Loads user data from googleapis service, such as name, given_name,
+    family_name, etc. as it's described in:
+    https://developers.google.com/accounts/docs/OAuth2Login
+    """
+    data = {'access_token': access_token, 'alt': 'json'}
+    request = Request(url + '?' + urlencode(data))
+    try:
+        return simplejson.loads(urlopen(request).read())
+    except (ValueError, KeyError, IOError):
+        return None
+
+
 def validate_whitelists(backend, email):
     """
     Validates allowed domains and emails against the following settings: