Repository: saleor/saleor

Issue:
facebook oauth login exception
I'm running into an exception using the FacebookClient oauth class as it is currently implemented in saleor.  I've set up the app access on facebook and using the facebook authenticate method from /login/ correctly produces the facebook dialogue requesting permission. However, the response isn't what's expected:  

`Django Version:    1.8.3`
`Exception Type:    KeyError`
`Exception Value:`
`'verified'`
`Exception Location:    [..]/saleor/registration/utils.py in get_user_info, line 186`

I've gone through the code and I can't tell if the facebook API has changed or if there's some misconfiguration on the facebook side.

The request URL that facebook returns is:

`Request URL:   http://localhost:5000/account/oauth_callback/facebook/?code=AQCDRZbizgMZ6bMjcUwk5_yRsvW4Ix10hrJsuqpJ_Ce5PVjoXyOpnBG27jSijFCD6kascY0NbhtSA3t1oQWyAjNwRser48t6oAQGxr-P7a_toXE-a96ctooe7BQ0vb_1_er5CVCa3E10w_rFQn-OkGgHpNPdrgMhe-XtRKrIx5kyEdUAP_dw1ndg3PBd3mxrvDU1W35ntqIZCZ_fNHM4Vj8pH-eONj6VljH9HTzZELpzJuRi81sklVnrr-K8Z0OnLksmf4erzixyIEhPBs_C2n_f4Shx_DDjfHvzKO-f5RkhgXq3mMw4LgEBZmlN0HM2AdU`

Can someone advise on how I might rectify this?  Thanks.



Discussion:
Thanks, @paweltin will look into this shortly.


Resolving patch:
diff --git a/saleor/registration/utils.py b/saleor/registration/utils.py
--- a/saleor/registration/utils.py
+++ b/saleor/registration/utils.py
@@ -156,7 +156,7 @@ def __init__(self, *args, **kwargs):
 
     def get_user_info(self):
         response = super(GoogleClient, self).get_user_info()
-        if response['verified_email']:
+        if response.get('verified_email'):
             return response
         else:
             raise ValueError('Google account not verified.')
@@ -171,7 +171,7 @@ class FacebookClient(OAuth2Client):
 
     auth_uri = 'https://www.facebook.com/dialog/oauth'
     token_uri = 'https://graph.facebook.com/oauth/access_token'
-    user_info_uri = 'https://graph.facebook.com/me'
+    user_info_uri = 'https://graph.facebook.com/me?fields=name,email,verified'
 
     scope = ','.join(['email'])
 
@@ -183,10 +183,11 @@ def __init__(self, *args, **kwargs):
 
     def get_user_info(self):
         response = super(FacebookClient, self).get_user_info()
-        if response['verified']:
-            return response
-        else:
+        if not response.get('verified'):
             raise ValueError('Facebook account not verified.')
+        if not response.get('email'):
+            raise ValueError('Access to your email address is required.')
+        return response
 
     def extract_error_from_response(self, response_content):
         return response_content['error']['message']
diff --git a/saleor/userprofile/models.py b/saleor/userprofile/models.py
--- a/saleor/userprofile/models.py
+++ b/saleor/userprofile/models.py
@@ -79,14 +79,6 @@ def __repr__(self):
 
 class UserManager(BaseUserManager):
 
-    def get_or_create(self, **kwargs):
-        defaults = kwargs.pop('defaults', {})
-        try:
-            return self.get_query_set().get(**kwargs), False
-        except self.model.DoesNotExist:
-            defaults.update(kwargs)
-            return self.create_user(**defaults), True
-
     def create_user(self, email, password=None, is_staff=False,
                     is_active=True, **extra_fields):
         'Creates a User with the given username, email and password'


Test patch: