Search code examples
pythongoogle-app-enginewebapp2

How to define a methode which splits the given url (In python webapp2 for Google app engine)


I want to set up a Google App Enging (GAE) app which provides a login functionality with OAuth2 and OAuth1 for Twitter, Facebook, ...., Therefore I chose the authomatic module (http://peterhudec.github.io/authomatic/) which seemd easy to use. But now I have a couple of problems (I am very new to that whole web service programming stuff).

So what I have is:

import os
import sys
import webapp2
from authomatic import Authomatic
from authomatic.adapters import Webapp2Adapter

from config import CONFIG

authomatic_dir = os.path.join(os.path.dirname(__file__), 'authomatic')
sys.path.append(authomatic_dir)

# Instantiate Authomatic.
authomatic = Authomatic(config=CONFIG, secret='some random secret string')

# Create a simple request handler for the login procedure.
class Login(webapp2.RequestHandler):
    # The handler must accept GET and POST http methods and
    # Accept any HTTP method and catch the "provider_name" URL variable.
    def any(self, provider_name):#HERE IS THE PROBLEM
        ...

class Home(webapp2.RequestHandler):
    def get(self):
        # Create links to the Login handler.
        self.response.write('Login with <a href="login/gl">Google</a>.<br />')


# Create routes.
ROUTES = [webapp2.Route(r'/login/gl', Login, handler_method='any'),
          webapp2.Route(r'/', Home)]


# Instantiate the webapp2 WSGI application.
application = webapp2.WSGIApplication(ROUTES, debug=True)

And the error I get is:

"any() takes exactly 2 arguments (1 given)"

I tried to substitute any with get() or post() because I already had an app where I did an redirect('blog/42') and the get(self, post_id) automatically split the 42 to post_id (example can be found here http://udacity-cs253.appspot.com/static/hw5.tgz (look at the PostPage class in blog.py))

So I really do not understand all the magic which happens here; could someone please explain me how to solve this error, so that the get()-parameter provider_name is assigned the value gl.


Solution

  • The handler method only takes arguments if you have <...> template variables in the route template. Each template variable populates an argument. For provider_name to be assigned gl where gl is part of the route path, change ROUTES to something like this:

    ROUTES = [webapp2.Route(r'/login/<:.*>', Login, handler_method='any'),
              webapp2.Route(r'/', Home)]
    

    More information: https://webapp-improved.appspot.com/api/webapp2.html#webapp2.Route