Skip to main content
 首页 » 编程设计

Python Eve 框架身份验证未应用于选定的 API

2025年05月04日9youxin

我正在用 Python Eve 编写 API,为了实现身份验证,我遵循了 Python Eve Authentication 上编写的文档

我的认证类代码如下:-

__author__ = 'sappal' 
 
from eve.auth import BasicAuth 
 
class SampleAuth(BasicAuth): 
  def check_auth(self, username, password, allowed_roles, resource, method): 
 
    if method == 'GET' and resource == 'org': 
      print "Inside GET Method for Org Resource" 
 
    elif method == 'POST' and resource == 'org': 
      print "Inside POST Method for Org Resource " 
 
    elif method == 'GET' and resource == 'people': 
      print "Inside GET Method for People Resource" 
 
    elif method == 'POST' and resource == 'people': 
      return True 
    else: 
      return username == 'admin' and password == 'secret' 

正如代码所述,我正在对人员端点进行 POST 调用而无需身份验证{这就是我打算做的},但是当我对人员端点进行 POST 调用时 [http://127.0.0.1:5000/people]。

我收到以下回复

{"_status": "ERR", "_error" 
     { 
       "message": "Please provide proper credentials",  
       "code": 401 
     } 
} 

我的请求负载是:-

[ 
  { 
    'firstname': 'Tushar', 
    'lastname': 'Sappal', 
    'email': 'testemailaddresshurray@gmail.com', 
    'phonenumber': 123456789, 
    'userid': '', 
    'password': 'testtushar' 
  } 
] 

我正在动态生成用户 ID,因此它不是在请求负载中传递的强制值。

请帮助和指导我,我做错了什么。

请您参考如下方法:

尝试为 people 资源将 public_methods 设置为 ['POST'](参见 docs )。或者您可以设置 PUBLIC_METHODS 但随后您将为所有 API 端点打开 POST 方法。