我正在使用Google提供的Oauth2装饰器。 现在我只是尝试使用 GAE 通过 Oauth2 对 Google 进行非常简单的登录。 我正在本地运行以进行测试,并且已成功通过 Google 进行身份验证;但是,在Google屏幕进行身份验证之前,它总是向我显示在本地主机上运行的本地登录屏幕(//localhost:14080/_ah/login?continue=http%3A//localhost%3A14080/)。 我不确定为什么我得到这个本地登录屏幕,它似乎与之后的 Google 登录屏幕没有任何关系。 我想知道如何避免此本地登录屏幕? 用于测试目的的非常简单的代码:
import webapp2
import jinja2
from apiclient.discovery import build
from google.appengine.api import users
from oauth2client.appengine import OAuth2Decorator
template_dir = os.path.join(os.path.dirname(__file__), "templates")
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir))
decorator = OAuth2Decorator(
client_id='the id given by google',
client_secret='the secret given by google',
scope='https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email')
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template,**kw))
class MainHandler(Handler):
@decorator.oauth_required
def get(self):
service = build('oauth2', 'v2', http=decorator.http())
request = service.userinfo().get().execute()
self.write(request["email"])
app = webapp2.WSGIApplication([
('/', MainHandler),
(decorator.callback_path, decorator.callback_handler())
], debug=True)
oauth2 装饰器依赖于让应用引擎登录的用户来运行(它使用 user-id 来存储 oauth2 凭据),因此如果不编写自己的代码,就无法避免出现屏幕 - 在生产中,登录名将被记住长达 30 天。