【发布时间】:2018-08-22 08:12:12
【问题描述】:
我有一个 GAE Python 应用程序,我正在从使用 App Engine 用户库过渡到使用 oauth2client 的 oauth2。
我阅读了一些关于在 App Engine 中使用 oauth2 的问题(例如,Managing users authentication in Google App Engine),用户库似乎不了解是否正在使用任何其他身份验证系统(即 oauth2)来对用户进行身份验证。
所以我打算使用 oauth2 而不是用户库来手动滚动我的登录/注销模型。然而,我惊讶地发现,当使用 oauth2client.appengine 库时,对 users 库的调用返回了来自 oauth2 身份验证用户的数据,如以下示例代码所示:
import webapp2
from google.appengine.api import users
from oauth2client.appengine import OAuth2Decorator
oauth2deco = OAuth2Decorator(...)
class TestOauth(webapp2.RequestHandler):
@oauth2deco.oauth_required
def get(self):
print users.get_current_user() # Prints user's email
app = webapp2.WSGIApplication([
('/', TestOauth),
(oauth2deco.callback_path, oauth2deco.callback_handler())
])
在上面的示例中,系统会提示用户使用 oauth2 登录屏幕(不是来自 users.CreateLoginURL 的传统 App Engine 登录屏幕)登录,而是调用 users.get_current_user( ) 按预期工作。
我很好奇,这是如何工作的?我看到 users.get_current_user() 函数返回了一个新的 User() 对象,它似乎读取了一些环境变量:https://cloud.google.com/appengine/docs/standard/python/refdocs/modules/google/appengine/api/users#User
但是,我从未见过这些是由 oauth2client 设置的,所以我仍然感到困惑。
【问题讨论】:
标签: google-app-engine google-oauth google-app-engine-python