使用PyFacebook和Google App Engine进行循环重定向
我有一个用Python做的Facebook项目,托管在Google App Engine上,我用下面的代码来初始化Facebook API,使用的是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)
我想设置一个装饰器,这样我就可以把它放在任何页面处理方法上,来检查用户的设置是否正确。不过问题是,当重定向处理程序被调用时,它会开始无限循环重定向。
我尝试用HTTP 302重定向来代替JavaScript,但那也一直失败。有没有人知道我该怎么解决这个问题?
我看到有一个类似的问题,但没有人回答。
2 个回答
0
2
今天我也遇到了同样的问题!我觉得发生的事情是,fbapi.check_session()没有正确设置fbapi.added。我认为Post-Add URL里面已经不再包含'installed'这个词了,但仍然有'fb_sig_added'。我在pyfacebook里做了以下的修改(类似于github上的代码),这解决了我遇到的无限重定向问题:
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