pyfacebook + Google App Engine:在 facebook.py 中找不到新功能

发布于 2024-10-14 19:45:22 字数 1472 浏览 3 评论 0原文

我正在尝试在 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

如果不是 facebookapi.check_connect_session(self.request): AttributeError:“Facebook”对象没有属性“check_connect_session”

所以它似乎可以很好地加载 facebook API,但不能加载我添加的新方法。我从开发者论坛复制并粘贴了 Facebook 类定义底部的代码,并确保所有缩进都是正确的,但似乎仍然没有找到它们。有谁知道可能是什么问题?

谢谢

I'm trying to use the pyfacebook functions (https://github.com/sciyoshi/pyfacebook/) in a Google app engine project. I've followed the advice on the Facebook developer forum (http://forum.developers.facebook.net/viewtopic.php?pid=164613) and added the additional functions to the __init__.py file, copied that file to the root directory of my project and renamed it facebook.py. Having imported facebook.py I added the following to the get(self) method for the Python class for the page:

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

When running it I get the following error:

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'

So it seems to be loading the facebook API fine, but not the new methods I've added. I copied and pasted the code from the developer forum at the bottom of the Facebook class definition, and made sure all the indentation was right but it still doesn't seem to be picking them up. Does anyone know what might be the problem?

Thanks

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2024-10-21 19:45:22

您相信 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 更新其代码时,您只需将新文件复制到位 - 无需合并您的自定义设置。

You believe the Facebook class has a certain method but Python is sure it hasn't. Why? Maybe you misspelled the method name, maybe you did not get the indentation right - hard to say without seeing the code.

You could try poking around to validate your assumptions:

import facebook
import logging

logging.warn('Facebook class: %r', dir(facebook.Facebook))
logging.warn('facebook module: %r', dir(facebook))

If you are sure you are operating on the correct file, the you should expect to see check_connect_session as a method of Facebook. If you didn't add enough indentation then you expect to see check_connect_method as a function defined in the facebook module. Too much indentation would make check_connect_method a sub function of which ever method precedes it and it won't show up in the above logging. Pay close attention to indentation.

However, a better way to add some custom methods might be:

import facebook

class Facebook(facebook.Facebook):
    def check_connect_session(request):
        pass

facebookapi = Facebook(API_KEY, SECRET)

if not facebookapi.check_connect_session(...):
    ...

Now when Facebook update their code you simply copy the new file into place - no need to merge your customisations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文