views:

415

answers:

2

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.

A: 

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

http://stackoverflow.com/questions/2039366/app-engine-patch-and-pyfacebook-not-working

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

Mathias Nielsen
+1  A: 

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
Fraser Harris
Thanks Fraser for the tip. This issue was haunting me for the longest time until I finally gave in and switched to minifb (actually gminifb http://blog.patrickcrosby.com/2008/04/20/Write-Facebook-apps-using-Google-AppEngine.html) over pyfacebook. I found it easier
Nick Gotch