飓风框架(FacebookGraphMixin)

3 投票
2 回答
1044 浏览
提问于 2025-04-17 11:05

我想试着通过我的Facebook应用,使用Tornado框架向用户发送应用请求。我在看这个链接:http://www.tornadoweb.org/documentation/auth.html,但是我不知道怎么解决这个错误。有没有专业人士能帮忙?谢谢!

错误日志

Traceback (most recent call last):
  File "send.py", line 36, in <module>
    main()
  File "send.py", line 33, in main
    test.get(app_access_token, player_id)
  File "send.py", line 15, in get
    callback=self.async_callback(self._on_post))
AttributeError: 'Send' object has no attribute 'async_callback'

代码

import tornado.auth
import tornado.escape
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado import httpclient

class Send(tornado.auth.FacebookGraphMixin):
    def get(self, app_access_token, player_id):
        self.facebook_request(
            "/"+player_id+"/apprequests",
            post_args={"message": "I am an app request from my Tornado application!"},
            access_token=app_access_token,
            callback=self.async_callback(self._on_post))

    def _on_post(self, new_entry):
        if not new_entry:
            # Call failed; perhaps missing permission?
            self.authorize_redirect()
            return
        self.finish("Posted a message!")

def main():
    key = "xxxxxxxxxxx"
    secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    player_id = "100003395454290" #fake id
    http_client = httpclient.HTTPClient()
    response = http_client.fetch("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id="+key+"&client_secret="+secret+"&redirect_uri=&code=")
    app_access_token = response.body.replace("access_token=","")

    test = Send()
    test.get(app_access_token, player_id)

if __name__ == "__main__":
    main()

2 个回答

0

看起来你忘了从 tornado.web.RequestHandler 这个类去继承了。你需要把下面的代码:

class Send(tornado.auth.FacebookGraphMixin):

改成:

class Send(tornado.web.RequestHandler, tornado.auth.FacebookGraphMixin):
1

好的,我的回答并没有直接解决提问者的问题。不过因为这个错误 AttributeError: 'XxxxxHandler' object has no attribute 'async_callback' 在搜索结果中排在前面,所以我想分享一些相关的信息。

值得注意的是,从 Tornado 4.0 版本开始,async_callback 这个函数就被移除了。引用一下 向后兼容性说明

RequestHandler.async_callbackWebSocketHandler.async_callback 这两个包装函数已经被移除;它们已经过时很久了,因为它们与堆栈上下文有关(最近也与协程有关)。

撰写回答