def __before__(self): """ From WSGIController documentation: This method is called before your action is, and should be used for setting up variables/objects, restricting access to other actions, or other tasks which should be executed before the action is called. """ c.is_ajax = request.environ.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest' or request.params.get('is_ajax') log.debug('request begin: %s/%s, %d', c.controller, c.action, int(time.time())) self._get_current_site_user() c.is_logged_in = bool(h.user())
def __before__(self): super(RequireUserController, self).__before__() if not h.user(): requested_url = h.url_for(controller=c.controller, action=c.action, qualified=True) args = {'client_id': h.fb_app_id(), 'redirect_uri': requested_url} user = None if request.params.get("code"): log.debug('found code, authorizing the user') args["client_secret"] = h.fb_secret() args["code"] = request.params["code"] graph_access_url = "https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(args) resp = urllib.urlopen(graph_access_url).read() resp = cgi.parse_qs(resp) if 'access_token' in resp: expires = int(resp['expires'][0]) fb_access_token_expiry = int(time.time()) + expires fb_access_token = resp["access_token"][-1] # get the user's id api = fblib.GraphAPI(fb_access_token) me = api.get_object('me') user = process_fb_user_data(me, fb_access_token, fb_access_token_expiry) # same as parent controller's _get_current_site_user() except we don't have # a cookie to use to get user info self._setup_helpers(user_id=user.user_id, fb_user_id=fb_user_id, fb_access_token=fb_access_token, user=user) if not user: log.debug('no user available, requesting login, url: %r', requested_url) args['scope'] = "email" #h.redirect_to("https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(args)) h.redirect_to("https://www.facebook.com/dialog/oauth?" + urllib.urlencode(args))