我正在用 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 方法。
关于Python Eve 框架身份验证未应用于选定的 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30130196/