Skip to main content
 首页 » 编程设计

python之无法从 Python EVE API 返回数据

2025年05月04日7落叶

这是我的 Python EVE 代码

from eve import Eve 
from eve.auth import BasicAuth 
 
class MyBasicAuth(BasicAuth): 
    def check_auth(self, username, password, allowed_roles, resource, 
                   method): 
        if resource == 'auth': 
            user = app.data.driver.db['user'] 
            user = user.find_one({'email': username,'password':password}) 
            if user: 
                return user 
            else: 
                False 
        else: 
            return True 
 
app = Eve(auth=MyBasicAuth) 
 
if __name__ == '__main__': 
    app.run() 

验证成功后,我将返回 user 对象,但未返回任何内容。我哪里错了。 这是settings.py文件

RESOURCE_METHODS = ['GET', 'POST'] 
 
DOMAIN = { 
    'user': { 
        'schema': { 
            'firstname': { 
                'type': 'string' 
            }, 
            'lastname': { 
                'type': 'string' 
            }, 
            'email': { 
                'type': 'string' 
            }, 
        'password': { 
        'type': 'string' 
        }, 
            'phone': { 
                'type': 'string' 
            } 
        } 
    }, 
    'auth': { 
        'schema': { 
            'username': { 
                'type': 'string' 
                }, 
            'password': { 
                'type':'string' 
                } 
            } 
        } 
} 

请您参考如下方法:

身份验证和授权与返回数据无关。您的 MyBasicAuth 类只应返回 True 或 False 以允许处理或拒绝当前请求。

假设您的客户端正在访问 user 端点,如果出现以下情况,它将返回数据:

  1. 您的自定义身份验证类返回 True

  2. 数据库中的 user 集合中有文档(您没有提供 datasource,因此 Eve 默认将端点作为数据源名称)。

此外,我不确定您为什么要测试 resource == 'auth',因为您没有在您的域中设置 auth 端点。

为了抢先一步,您可能需要检查 QuickStart首先,然后 Authentication and Authorization最后是 RESTful Account Management教程。