Skip to main content
 首页 » 编程设计

python之DRF API 端点获取 "Authentication credentials were not provided"

2024年12月31日15zdz8207

我正在使用 DRF 构建一些 API 端点。这是代码片段。

class ReportsDashboardView(views.APIView): 
 permission_classes = ( 
        IsAuthenticated, IsActiveUser, 
 ) 
 
 def get(self, request, format=None): 
  ... 

我尝试通过 postman 基本身份验证调用 API。但它总是返回“未提供身份验证凭据”。 这是身份验证配置的代码片段。

REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'] = ( 
    'rest_framework.authentication.SessionAuthentication', 
    'rest_framework.authentication.TokenAuthentication', 
    'rest_framework.authentication.BasicAuthentication', 
) 

我不确定为什么基本身份验证(用户名、密码)对我不起作用。 这是你以前遇到的事情吗?你愿意帮助我吗?

请您参考如下方法:

确保您使用的凭据是正确的,并且允许用户在两​​种权限级别下进行访问

来自docs (强调我的)

If any permission check fails an exceptions.PermissionDenied or exceptions.NotAuthenticated exception will be raised, and the main body of the view will not run.