如何定义一个方法来分割给定的url(在python webapp2中用于谷歌应用程序引擎)



我想建立一个谷歌应用程序引擎(GAE)应用程序,它为Twitter、Facebook……提供OAuth2和OAuth1的登录功能。。。。,因此,我选择了authomatic模块(http://peterhudec.github.io/authomatic/)看起来很容易使用。但现在我遇到了一些问题(我对整个web服务编程还很陌生)。

所以我有:

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)

我得到的错误是:

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

我试着用get()或post()代替any,因为我已经有了一个应用程序,我在那里做了redirect('blog/42')CCD_ 2自动将CCD_ 3拆分为CCD_http://udacity-cs253.appspot.com/static/hw5.tgz(查看blog.py中的PostPage类)

所以我真的不理解这里发生的所有魔法;有人能告诉我如何解决这个错误吗,这样get()-参数provider_name就被分配了值gl

而不是

webapp2.Route(r'/login/gl', Login, handler_method='any')

使用

webapp2.Route(r'/login/<provider_name>', Login, handler_method='any')

现在/login/之后的路径将在provider_name参数中传递给def any

即请求/login/gl将把"gl"作为provider_name传递给def any

如果路由模板中有<...>模板变量,则处理程序方法只接受参数。每个模板变量都会填充一个参数。对于要分配给provider_namegl,其中gl是路由路径的一部分,请将ROUTES更改为以下内容:

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

更多信息:https://webapp-improved.appspot.com/api/webapp2.html#webapp2.Route

最新更新