- API_KEY = str(settings.FB_API_KEY)
-
- # Check if the Facebook cookie has been received.
- if 'fbs_%s' % API_KEY in request.COOKIES:
- fbs_cookie = request.COOKIES['fbs_%s' % API_KEY]
- parsed_fbs = parse_qs(smart_unicode(fbs_cookie))
- self.parsed_fbs = parsed_fbs
-
- # Check if the session hasn't expired.
- if self.check_session_expiry(request.COOKIES):
- return parsed_fbs['uid'][0]
- else:
- raise InvalidAuthentication(_('Sorry, your Facebook session has expired, please try again'))
- else:
- raise InvalidAuthentication(_('The authentication with Facebook connect failed, cannot find authentication tokens'))
- def check_session_expiry(self, cookies):
- return datetime.fromtimestamp(float(self.parsed_fbs['expires'][0])) > datetime.now()
-
- def get_user_data(self, cookies):
- API_KEY = str(settings.FB_API_KEY)
- fbs_cookie = cookies['fbs_%s' % API_KEY]
- parsed_fbs = parse_qs(smart_unicode(fbs_cookie))
-
- # Communicate with the access token to the Facebook oauth interface.
- json = load_json(urlopen('https://graph.facebook.com/me?access_token=%s' % parsed_fbs['access_token'][0]))
+ try:
+ args = dict(client_id=settings.FB_API_KEY, redirect_uri="%s%s" % (django_settings.APP_URL, request.path))
+
+ args["client_secret"] = settings.FB_APP_SECRET #facebook APP Secret
+
+ args["code"] = request.GET.get("code", None)
+ response = cgi.parse_qs(urlopen("https://graph.facebook.com/oauth/access_token?" + urlencode(args)).read())
+ access_token = response["access_token"][-1]
+
+ # Store the access token in cookie
+ request.session["assoc_key"] = access_token
+
+ return access_token
+ except Exception, e:
+ logging.error("Problem during facebook authentication: %s" % e)
+ raise InvalidAuthentication(_("Something wrond happened during Facebook authentication, administrators will be notified"))
+
+ def get_user_data(self, assoc_key):
+ profile = load_json(urlopen("https://graph.facebook.com/me?" + urlencode(dict(access_token=assoc_key))))
+
+ name = profile["name"]
+
+ # Check whether the length if the email is greater than 75, if it is -- just replace the email
+ # with a blank string variable, otherwise we're going to have trouble with the Django model.
+ email = smart_unicode(profile['email'])
+ if len(email) > 75:
+ email = ''
+
+ # If the name is longer than 30 characters - leave it blank
+ if len(name) > 30:
+ name = ''