2011-01-26 67 views
0

我正尝试在Google应用引擎项目中使用pyfacebook函数(https://github.com/sciyoshi/pyfacebook/)。我遵循Facebook开发者论坛上的建议(http://forum.developers.facebook.net/viewtopic.php?pid=164613)并将其他函数添加到__init__.py文件中,将该文件复制到根目录我的项目目录并将其重命名为facebook.py。已经进口facebook.py添加以下的get(个体经营)方法的Python类的页:pyfacebook + Google App Engine:在facebook.py中找不到新功能

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)) 

当运行它,我得到以下错误:

File "\much\baw08u\Private\IDS\helloworld\helloworld.py", line 54, in get

if not facebookapi.check_connect_session(self.request): AttributeError: 'Facebook' object has no attribute 'check_connect_session'

如此看来要加载facebook API的罚款,但不是我添加的新方法。我复制并粘贴了Facebook类定义底部的开发者论坛中的代码,并确保所有缩进都是正确的,但它似乎仍然没有被选中。有谁知道可能是什么问题?

感谢

+0

你是否已经将方法添加到Facebook.Facebook(应该工作)或功能到Facebook(不应该工作,除非你使用facebook.function_name)? – TryPyPy 2011-01-26 16:06:18

+0

是的,这一切都在facebook.Facebook - 检查缩进和一切。有没有人有修改的\ __ init__.py的副本,我可以测试以查看它是否与语法有关? – benwad 2011-01-27 12:29:29

回答

2

你相信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的更新他们的代码,你的新文件简单地复制到的地方 - 没有需要合并你的定制。