Search code examples
pythongoogle-app-enginepython-2.7webapp2

GAE/Python - redirect working from main class but not from called method


When I make a redirect from main.py, it works, but when I try to redirect from within a method that it calls, nothing happens. There is no error, the program simply does nothing.

main.py

from githubauth import GetAuthTokenHandler 

class AuthUser(webapp2.RequestHandler):
    """
    If no environment variable exists with the access token in it,
    auth the user as an admin. If it does exist, auth them as a regular
    user.
    """

    def get(self):

        if not ACCESS_TOKEN:
            # No access token exists, auth user as admin 
            get_auth_token = GetAuthTokenHandler() 
            get_auth_token.get()

githubauth.py

import webapp2

class GetAuthTokenHandler(webapp2.RequestHandler):
    """Redirect users to github to get an access request token."""

    def get(self):
        self.redirect('http://api.github.com/authorize')

Solution

  • You try to create a webapp2 request handler, but it cannot be done this way. get_auth_token is not a WSGI webapp2 handler instance. If you do not change githubauth.py you have to change your main.py.

    class AuthUser(webapp2.RequestHandler):
    
    def get(self):
    
        if not ACCESS_TOKEN:
            self.redirect(to your GetAuthTokenHandler)
    

    This will result in two redirects if you do not have an access token.