diff --git a/socialauth/auth_backends.py b/socialauth/auth_backends.py index 1f42ca1..1d00fc0 100644 --- a/socialauth/auth_backends.py +++ b/socialauth/auth_backends.py @@ -1,6 +1,7 @@ from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.conf import settings +from django.utils import simplejson as json import facebook import urllib @@ -8,6 +9,7 @@ from socialauth.models import OpenidProfile as UserAssociation, TwitterUserProfile, FacebookUserProfile, LinkedInUserProfile, AuthMeta from socialauth.lib.linkedin import * + import random TWITTER_CONSUMER_KEY = getattr(settings, 'TWITTER_CONSUMER_KEY', '') @@ -222,7 +224,7 @@ def authenticate(self, request, user=None): params = {} params["client_id"] = FACEBOOK_APP_ID params["client_secret"] = FACEBOOK_SECRET_KEY - params["redirect_uri"] = reverse("socialauth_facebook_login_done")[1:] + params["redirect_uri"] = request.build_absolute_uri(reverse("socialauth_facebook_login_done")) params["code"] = request.GET.get('code', '') url = "https://graph.facebook.com/oauth/access_token?"+urllib.urlencode(params) @@ -233,34 +235,37 @@ def authenticate(self, request, user=None): if not res_parse_qs.has_key('access_token'): return None - parse_data = res_parse_qs['access_token'] - uid = parse_data['uid'][-1] - access_token = parse_data['access_token'][-1] + access_token = res_parse_qs['access_token'][-1] + + graph = facebook.GraphAPI(access_token) + fb_data = graph.get_object("me") + + if not fb_data: + return None + + uid = fb_data['id'] + username = uid try: fb_user = FacebookUserProfile.objects.get(facebook_uid=uid) return fb_user.user except FacebookUserProfile.DoesNotExist: - # create new FacebookUserProfile - graph = facebook.GraphAPI(access_token) - fb_data = graph.get_object("me") - - if not fb_data: - return None - - username = uid if not user: user = User.objects.create(username=username) user.first_name = fb_data['first_name'] user.last_name = fb_data['last_name'] + is_email_filled = False + if 'email' in fb_data: + user.email = fb_data['email'] + is_email_filled = True user.save() fb_profile = FacebookUserProfile(facebook_uid=uid, user=user) fb_profile.save() - auth_meta = AuthMeta(user=user, provider='Facebook').save() + auth_meta = AuthMeta(user=user, provider='Facebook', is_email_filled=is_email_filled).save() return user diff --git a/socialauth/views.py b/socialauth/views.py index 2b4cf9a..cbd2784 100644 --- a/socialauth/views.py +++ b/socialauth/views.py @@ -32,6 +32,7 @@ FACEBOOK_APP_ID = getattr(settings, 'FACEBOOK_APP_ID', '') FACEBOOK_API_KEY = getattr(settings, 'FACEBOOK_API_KEY', '') FACEBOOK_SECRET_KEY = getattr(settings, 'FACEBOOK_SECRET_KEY', '') +FACEBOOK_EXTENDED_PERMISSIONS = getattr(settings, 'FACEBOOK_EXTENDED_PERMISSIONS', []) def del_dict_key(src_dict, key): @@ -154,7 +155,7 @@ def openid_login(request): return begin(request) def gmail_login(request): - request.session['openid_provider'] = 'Google' + request.session['openid_provider'] = 'google' return begin(request, user_url='https://www.google.com/accounts/o8/id') def gmail_login_complete(request): @@ -213,6 +214,7 @@ def facebook_login(request): params = {} params["client_id"] = FACEBOOK_APP_ID params["redirect_uri"] = request.build_absolute_uri(reverse("socialauth_facebook_login_done")) + params["scope"] = ",".join(FACEBOOK_EXTENDED_PERMISSIONS) url = "https://graph.facebook.com/oauth/authorize?"+urllib.urlencode(params)