Looping Redirect with PyFacebook and Google App Engine

Posted by Nick Gotch on Stack Overflow See other posts from Stack Overflow or by Nick Gotch
Published on 2010-02-27T23:28:16Z Indexed on 2010/03/18 4:41 UTC
Read the original article Hit count: 608

I have a Python Facebook project hosted on Google App Engine and use the following code to handle initialization of the Facebook API using PyFacebook.

# Facebook Initialization
def initialize_facebook(f):

    # Redirection handler
    def redirect(self, url):
        logger.info('Redirecting the user to: ' + url)
        self.response.headers.add_header("Cache-Control", "max-age=0")
        self.response.headers.add_header("Pragma", "no-cache")
        self.response.out.write('<html><head><script>parent.location.replace(\'' + url + '\');</script></head></html>')
        return 'Moved temporarily'

    auth_token = request.params.get('auth_token', None)
    fbapi = Facebook(settings['FACEBOOK_API_KEY'], settings['FACEBOOK_SECRET_KEY'], auth_token=auth_token)

    if not fbapi:
        logger.error('Facebook failed to initialize')

    if fbapi.check_session(request) or auth_token:
        pass
    else:
        logger.info('User not logged into Facebook')
        return lambda a: redirect(a, fbapi.get_login_url())

    if fbapi.added:
        pass
    else:
        logger.info('User does not have ' + settings['FACEBOOK_APP_NAME'] + ' added')
        return lambda a: redirect(a, fbapi.get_add_url())

    # Return the validated API
    logger.info('Facebook successfully initialized')
    return lambda a: f(a, fbapi=fbapi)

I'm trying to set it up so that I can drop this decorator on any page handler method and verify that the user has everything set up correctly. The issue is that when the redirect handler gets called, it starts an infinite loop of redirection.

I tried using an HTTP 302 redirection in place of the JavaScript but that kept failing too. Does anyone know what I can do to fix this?

I saw this similar question but there are no answers.

© Stack Overflow or respective owner

Related posts about google-app-engine

Related posts about pyfacebook