使用tornado.auth.TwitterMixin时的回调URL?

1 投票
1 回答
750 浏览
提问于 2025-04-16 12:12

我正在尝试使用tornado.auth.TwitterMixin,并且想设置一个回调网址,但遇到了一些问题。
我不太确定如何在Tornado应用程序中设置这个回调网址。
下面是我为tornado.auth.TwitterMixin写的类:

class TAuthBindingHandler(BaseHandler,tornado.auth.TwitterMixin): 
    @tornado.web.asynchronous 
    def get(self): 
        if self.get_argument("oauth_token", None): 
self.get_authenticated_user(self.async_callback(self._on_auth)) 
            return 
        self.authorize_redirect() 
    def _on_auth(self, user): 
        if not user: 
            raise tornado.web.HTTPError(500, "Twitter auth failed") 
        tuser = self.db.get("SELECT * FROM twitterusers WHERE tid = 
%s",user["id"]) 
        bigU = self.get_current_user() 
        bigU_id = bigU['id'] 
        if not tuser: 
            any_tuser = self.db.get("SELECT * FROM twitterusers LIMIT 
1") 
            if not any_tuser: 
                tuser_id = self.db.execute( 
                    "INSERT INTO twitterusers (name,tid,user_id) 
VALUES (%s,%s,%s)", 
                    user["name"], user["id"], bigU_id) 
            else: 
                self.redirect("/") 
                return 
        else: 
            pass 
        self.redirect(self.get_argument("next", "/"))

我的问题是,我应该在哪里设置回调网址?在这个类中我该怎么设置?

我使用的是Tornado 1.1,并且在我的Twitter应用设置中没有设置任何回调。

我是在本地进行测试。

最好的问候。

1 个回答

2

嘿,我不确定你是否还需要这个答案,但 self.authorize_redirect 需要一个 callback_uri。所以在你的情况下,我会写 self.authorize_redirect('http://localhost:8888/authentication-complete')。我花了一段时间才弄明白这个。祝你好运!

撰写回答