Question

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.

Was it helpful?

Solution

I was just having the exact same thing happen to me today! What I think is happening is that fbapi.check_session() is not setting fbapi.added correctly. I don't think the Post-Add URL contains 'installed' anymore, but still has 'fb_sig_added'. The following change (github-esque code) in pyfacebook stopped the infinite redirect for me:

1244 | 1244 |     if request.method == 'POST':
1245 | 1245 |         params = self.validate_signature(request.POST)
1246 | 1246 |     else:
1247 |      | -       if 'installed' in request.GET:
     | 1247 | +       if 'installed' in request.GET or request.GET['fb_sig_added'] == '1':
1248 | 1248 |             self.added = True

OTHER TIPS

The problem seems familiar. May you can use the solution from my question here:

app-engine-patch and pyFacebook not working

But that was with the decorator that comes with pyfacebook so it might be different.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top