Python/WebApp Google App Engine - 测试用户/传递标题

Nea*_*ers 4 python google-app-engine web-applications

当您调用这样的Web服务时:

   username = 'test12'
   password = 'test34' 
   client = httplib2.Http(".cache") 
   client.add_credentials(username,password)
   URL = "http://localhost:8080/wyWebServiceTest"
   response, content = client.request(URL) 
Run Code Online (Sandbox Code Playgroud)

如何在服务器端(即我正在编写的Web服务中)将用户名/密码转换为变量.我检查了self.request.headers和self.request.environ并找不到它们.

(我没有使用Google登录,需要将此用户ID /传递反弹到我自己的数据库以验证安全性.)

我试图从这个页面的想法:http://pythonpaste.org/webob/reference.html#headers

谢谢,

尼尔沃尔特斯

下面对Peter的代码略有增强:

 auth = None 
 if 'Authorization' in self.request.headers: 
    auth = self.request.headers['Authorization']
 if not auth:
Run Code Online (Sandbox Code Playgroud)

Pet*_*son 7

我没有测试过这段代码(插入笑脸),但我认为这是你需要的东西.如果您的服务器没有将401退回到您的客户端(客户端需要知道领域以了解要提供的凭据),基本上您的凭据将不在标题中.

class MYREALM_securepage(webapp.RequestHandler):
  def get(self):
      if not 'Authorization' in self.request.headers:
          self.response.headers['WWW-Authenticate'] = 'Basic realm="MYREALM"'
          self.response.set_status(401)
          self.response.out.write("Authorization required")
      else:
          auth = self.request.headers['Authorization']
          (username, password) = base64.b64decode(auth.split(' ')[1]).split(':')
          # Check the username and password, and proceed ...
Run Code Online (Sandbox Code Playgroud)