如何绕过Oauth2和GAE的本地登录屏幕 [英] How to Bypass Local Login Screen with Oauth2 and GAE
问题描述
import webapp2
从apiclient.discovery导入jinja2
import build
from google.appengine.api从oauth2client.appengine导入用户
导入OAuth2Decorator
template_dir = os.path.join(os.path.dirname(__ file__), 模板)
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir))
decorator = OAuth2Decorator(
client_id ='id给出的google',
client_secret ='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)
$ b $ 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装饰器依赖于使用appengine登录的用户来运行(它使用用户标识来存储oauth2凭证),所以无需编写自己的代码,就不可能避免出现屏幕 - 在生产中,登录将被记住长达30天。
I am working with the Oauth2 Decorator provided by Google. Right now I am just trying to do a very simple login via Oauth2 to Google using GAE. I am running locally for test purposes and have been successful in authenticating with Google; however, prior to the Google screen for authentication it always presents me with a local login screen running on localhost (//localhost:14080/_ah/login?continue=http%3A//localhost%3A14080/). I am not sure why I am getting this local login screen which does not appear to have any bearing on the Google login screen that comes after. I am wondering how to avoid this local login screen? Very simple code for test purposes:
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)
The oauth2 decorator relies on having an appengine-logged-in user to function (it uses the user-id to store the oauth2 credentials), so without writing your own code, it isn't possible to avoid having the screen appear - in production, the login will be remembered for up to 30 days.
这篇关于如何绕过Oauth2和GAE的本地登录屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!