我目前正在使用龍捲風來顯示我的Twitter流。下面是我的代碼:龍捲風驗證(Twittermixin)問題
#!/usr/bin/env python
import time
import logging
from tornado.auth import TwitterMixin
from tornado.escape import json_decode, json_encode
from tornado.ioloop import IOLoop
from tornado import gen
from tornado.options import define, options, parse_command_line, parse_config_file
from tornado.web import Application, RequestHandler, authenticated, HTTPError
define('port', default=8080, help="port to listen on")
define('config_file', default='secrets.cfg',
help='filename for additional configuration')
define('debug', default=True, group='application',
help="run in debug mode (with automatic reloading)")
# The following settings should probably be defined in secrets.cfg
define('twitter_consumer_key', type=str, group='application')
define('twitter_consumer_secret', type=str, group='application')
define('cookie_secret', type=str, group='application',
default='this is a string',
help="signing key for secure cookies")
class BaseHandler(RequestHandler):
COOKIE_NAME = "uuser"
def get_current_user(self):
user_json = self.get_secure_cookie(self.COOKIE_NAME)
if not user_json:
print(" No user_json")
return None
print(" Yes user_json")
return json_decode(user_json)
class MainHandler(BaseHandler, TwitterMixin):
@authenticated
@gen.coroutine
def get(self):
timeline = yield self.twitter_request(
'/statuses/home_timeline',
access_token = self.current_user['access_token'])
self.render('home.html', timeline=timeline)
class LoginHandler(BaseHandler, TwitterMixin):
@gen.coroutine
def get(self):
if self.get_argument('oauth_token', None):
user = yield self.get_authenticated_user()
print(' user:', type(user))
del user["description"]
self.set_secure_cookie(self.COOKIE_NAME, json_encode(user))
print(' get_secure_cookie:', self.get_secure_cookie(self.COOKIE_NAME))
self.redirect(self.get_argument('next', '/'))
else:
print(" Authorize_redirecting...")
yield self.authorize_redirect(callback_uri=self.request.full_url())
class LogoutHandler(BaseHandler):
def get(self):
self.clear_cookie("user")
def main():
parse_command_line(final=False)
parse_config_file(options.config_file)
app = Application(
[
(r'/', MainHandler),
(r'/login', LoginHandler),
(r'/logout', LogoutHandler),
],
login_url='/login',
**options.group_dict('application'))
app.listen(options.port)
logging.info('Listening on http://localhost:%d' % options.port)
IOLoop.current().start()
if __name__ == '__main__':
main()
所以我流的理解如下:
1)訪問「/」 - MainHandler,該@authenticated
將重定向到login_url
如果用戶沒有登錄。
2)訪問 '/登錄' - LoginHandler,self.authorize_redirect(callback_uri=self.request.full_url())
將在URL的末尾添加的oauth_token參數,並重新訪問 '/登錄'
3)訪問 '/登錄' - LoginHandler,獲得用戶self.get_authenticated_user()
,和set_secure_cookie(self.COOKIE_NAME, json_encode(user))
這裏是我想這個問題,我似乎無法設置Cookie。當我嘗試立即self.get_secure_cookie(self.COOKIE_NAME)
訪問它,它返回None
,因此,不斷重新訪問「/登錄」
任何人能提供給我的問題有所幫助?也許這是非常明顯的,我沒有看到。由於
我還設置http://127.0.0.1:8080/
作爲回調URL我的Twitter的應用程序設置,不知道這是否有該問題的任何貢獻。
好吧,我相信'set_secure_cookie(名稱= self.COOKIE_NAME,值= json_encode(用戶))'是問題的原因,它從來沒有設法設置cookie的在我的瀏覽器。我已經檢查了'user'對象存在且是一個'dict'和'json_encode'不會返回一個字符串值...所以都必須有'set_secure_cookie'功能的東西.... – creampiedonut