Tornado Google OAuth 错误

0 投票
1 回答
515 浏览
提问于 2025-04-30 23:14

我遇到了一个关于 tornado.oauth 的问题:当用户用谷歌登录时,谷歌会把一个代码发送到我的重定向地址,然后我尝试用 get_authenticated_user 这个函数获取用户的一些信息。

class GoogleOAuth2CodeHandler(tornado.web.RequestHandler,
                           tornado.auth.GoogleOAuth2Mixin):
    @tornado.gen.coroutine
    def get(self):
        user =yield self.get_authenticated_user(
            redirect_uri='http://localhost:8890/userdata',
            code=self.get_argument("code")
        )
        self.write("hello world!")

但是它出现了以下错误:

ERROR:tornado.application:Uncaught exception GET
KeyError: 'google_oauth'

设置:

settings = dict(
        cookie_secret="32oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
        login_url="/auth/login",
        redirect_uri="http://localhost:8890/auth",
        google_consumer_key="",
        google_consumer_secret="",
        google_permissions="https://mail.google.com/ https://www.google.com/m8/feeds",
        google_permissions2="https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"
    ))

consumer_keyconsumer_secret 是正确的,authorize_redirect 也正常工作,我确实收到了正确的谷歌代码。

暂无标签

1 个回答

2

google_consumer_keygoogle_consumer_secret 是来自 OAuth1 的 Google 混合设置。如果你使用的是 oauth2,那么你需要一个 google_oauth 的设置,这个设置应该是一个字典,里面包含 'key' 和 'secret' 这两个字段:

  google_oauth={"key": CLIENT_ID, "secret": CLIENT_SECRET},

撰写回答