Google 使用 Tornado 进行身份验证
Google Authentication with Tornado
我正在尝试使用 Tornado 获得 google 身份验证来测试这个概念。我有以下代码:
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.gen
import tornado.auth
import os.path
import time
from tornado.options import define, options
define("port",default=8000, help="run on port", type=int)
class AuthHandler(tornado.web.RequestHandler, tornado.auth.GoogleMixin):
@tornado.gen.coroutine
def get(self):
if self.get_argument('code', False):
print("now running auth_user()")
user = yield self.get_authenticated_user(
redirect_uri='http://localhost:8000/auth',
code=self.get_argument('code'))
self.set_secure_cookie('token',"XXX")
self.redirect('/')
else:
yield self.authorize_redirect(
redirect_uri='http://localhost:8000/auth',
client_id=self.settings['google_oauth']['key'],
scope=['profile', 'email'],
response_type='code',
extra_params={'approval_prompt': 'force'})
# def post(self):
# self.render('index.html')
class IndexHandler(tornado.web.RequestHandler):
def get(self):
token=self.get_secure_cookie('token')
if not token:
print("getting google token")
self.redirect('/auth')
else:
print("TOKEN!")
# def post(self):
# self.render('index.html')
if __name__ == '__main__':
tornado.options.parse_command_line()
H=[(r'/',IndexHandler),(r'/auth',AuthHandler)]
T=os.path.join(os.path.dirname(__file__),"templates")
settings=dict(
google_oauth = dict(key="XXX.apps.googleusercontent.com", secret ="YYYYY"),
cookie_secret = 'DPTDQARHTDayv8WV61iMSsvAD18Rc00bizO519+2i4w='
)
app = tornado.web.Application(handlers=H,template_path=T,debug=True, **settings)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
我一直收到
"TypeError: authorize_redirect() got an unexpected keyword argument 'client_id'"
我按照有关获取客户端 ID 和密码的文档进行操作,并将这些值放入设置中。此外,我已将 google 控制台中的重定向 URI 设置为 'localhost:8000/auth'
应该是
class AuthHandler(tornado.web.RequestHandler, tornado.auth.GoogleOAuth2Mixin):
我正在尝试使用 Tornado 获得 google 身份验证来测试这个概念。我有以下代码:
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.gen
import tornado.auth
import os.path
import time
from tornado.options import define, options
define("port",default=8000, help="run on port", type=int)
class AuthHandler(tornado.web.RequestHandler, tornado.auth.GoogleMixin):
@tornado.gen.coroutine
def get(self):
if self.get_argument('code', False):
print("now running auth_user()")
user = yield self.get_authenticated_user(
redirect_uri='http://localhost:8000/auth',
code=self.get_argument('code'))
self.set_secure_cookie('token',"XXX")
self.redirect('/')
else:
yield self.authorize_redirect(
redirect_uri='http://localhost:8000/auth',
client_id=self.settings['google_oauth']['key'],
scope=['profile', 'email'],
response_type='code',
extra_params={'approval_prompt': 'force'})
# def post(self):
# self.render('index.html')
class IndexHandler(tornado.web.RequestHandler):
def get(self):
token=self.get_secure_cookie('token')
if not token:
print("getting google token")
self.redirect('/auth')
else:
print("TOKEN!")
# def post(self):
# self.render('index.html')
if __name__ == '__main__':
tornado.options.parse_command_line()
H=[(r'/',IndexHandler),(r'/auth',AuthHandler)]
T=os.path.join(os.path.dirname(__file__),"templates")
settings=dict(
google_oauth = dict(key="XXX.apps.googleusercontent.com", secret ="YYYYY"),
cookie_secret = 'DPTDQARHTDayv8WV61iMSsvAD18Rc00bizO519+2i4w='
)
app = tornado.web.Application(handlers=H,template_path=T,debug=True, **settings)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
我一直收到
"TypeError: authorize_redirect() got an unexpected keyword argument 'client_id'"
我按照有关获取客户端 ID 和密码的文档进行操作,并将这些值放入设置中。此外,我已将 google 控制台中的重定向 URI 设置为 'localhost:8000/auth'
应该是
class AuthHandler(tornado.web.RequestHandler, tornado.auth.GoogleOAuth2Mixin):