How to Bypass Local Login Screen with Oauth2 and G

2019-05-28 04:13发布

问题:

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)

回答1:

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.