pyfacebook + Google App Engine:无法在facebook.py中找到新函数
我正在尝试在一个Google应用引擎项目中使用pyfacebook的功能(https://github.com/sciyoshi/pyfacebook/)。我按照Facebook开发者论坛上的建议(http://forum.developers.facebook.net/viewtopic.php?pid=164613)将额外的功能添加到了__init__.py文件中,然后把这个文件复制到我的项目根目录,并把它重命名为facebook.py。在导入了facebook.py后,我在页面的Python类的get(self)方法中添加了以下内容:
facebookapi = facebook.Facebook(API_KEY, SECRET)
if not facebookapi.check_connect_session(self.request):
path = os.path.join(os.path.dirname(__file__), 'templates/login.html')
self.response.out.write(template.render(path, {'apikey': API_KEY}))
return
user = facebookapi.users.getInfo(
[facebookapi.uid],
['uid', 'name', 'birthday', 'relationship_status'])[0]
template_values = {
'name': user['name'],
'birthday': user['birthday'],
'relationship_status': user['relationship_status'],
'uid': user['uid'],
'apikey': API_KEY
}
path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
self.response.out.write(template.render(path, template_values))
运行时我遇到了以下错误:
文件 "\much\baw08u\Private\IDS\helloworld\helloworld.py",第54行,在get中
if not facebookapi.check_connect_session(self.request): AttributeError: 'Facebook'对象没有属性'check_connect_session'
看起来facebook API加载得很好,但我添加的新方法却没有被识别。我从开发者论坛复制并粘贴了代码到Facebook类定义的底部,并确保所有的缩进都是正确的,但它似乎还是没有识别这些方法。有人知道可能是什么问题吗?
谢谢
本
1 个回答
你觉得 Facebook
这个类应该有某个方法,但 Python 却说没有。为什么会这样呢?可能是你拼错了方法名,或者缩进没有对齐——不看代码很难说清楚。
你可以尝试检查一下自己的假设:
import facebook
import logging
logging.warn('Facebook class: %r', dir(facebook.Facebook))
logging.warn('facebook module: %r', dir(facebook))
如果你确定自己在正确的文件上操作,那么你应该能看到 check_connect_session 作为 Facebook 的一个方法。如果缩进不够,你可能会看到 check_connect_method 作为 facebook 模块中定义的一个函数。如果缩进过多,check_connect_method 就会变成前面某个方法的子函数,这样在上面的日志中就看不到它了。一定要注意缩进哦。
不过,添加一些自定义方法的更好方式可能是:
import facebook
class Facebook(facebook.Facebook):
def check_connect_session(request):
pass
facebookapi = Facebook(API_KEY, SECRET)
if not facebookapi.check_connect_session(...):
...
这样,当 Facebook 更新他们的代码时,你只需要把新文件复制过来——不需要合并你的自定义内容。