Jake Runzer Jake Runzer - 1 year ago 127
HTML Question

Google App Engine Redirect to User Login on Button Click

I have a Python script in Google App Engine that just gives a simple welcome message to the user. If the user is logged into Google then there is a personalized welcome message with the code:

if users.get_current_user():
self.response.out.write('Welcome ' + users.get_current_user()')

What I want to do is if the user is not already logged in, have a button that can be pressed which will redirect them to the Google login page to login to their account.

#redirect user to login page here

I have tried some ways to do this but no of them will work. I have tried:

self.response.out.write('<form action="%s">
<input type="submit" value="Login to Google"></form>
' % users.create_login_url('/')

When the button is pressed nothing happens so I tried:

self.response.out.write('<form action="%s">
<input type="submit" value="Login to Google"></form>
' % self.redirect(users.create_login_url('/'))

This just redirects the user to the login page as soon as the main page is visited.

Is there any way to redirect the user to the Google login page when a button is pressed?

Answer Source

What you are missing is a handler that checks the url and redirects the user:

so for form action you would have to change it to:

    signin_page = "/signin/"
    self.response.write('<form action="%s">
                         <input type="submit" value="Login to Google"></form>'
                         % signin_page)

Then in the handler:

 class SignInHandler(webapp2.RequestHandler):
    def get(self):

and finally:

 application = webapp2.WSGIApplication([('/', IndexPageHandler),
                                        ('/signin/', SignInHandler),],)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download