rest_framework_swagger安装- HTTP 401未经授权



我已经成功安装了rest_framework_swagger,之后我在urls.py文件项目中设置了url:

from django.contrib import admin
from django.urls import path, include, re_path
from unimiproject.router import router
from rest_framework.authtoken import views
from rest_framework.schemas import get_schema_view
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
re_path(r"^appjud/",include("appjud.urls")),
path('api/', include(router.urls)),
path('api-token-auth/', views.obtain_auth_token, name='api-token-auth'),
path('openapi/', get_schema_view(
title="School Service",
description="API developers hpoing to use our service"
), name='openapi-schema'),
path('docs/', TemplateView.as_view(
template_name='documentation.html',
extra_context={'schema_url':'openapi-schema'}
), name='swagger-ui')
]

但是如果我浏览http://172.18.0.1:7000/openapi/我得到这个:

Schema
GET /openapi/
HTTP 401 Unauthorized
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
WWW-Authenticate: Token
{
"detail": "Authentication credentials were not provided."
}

为什么我得到"未提供身份验证凭据"?它应该显示所有api的模式,而不是测试它们。

尝试在代码中使用permission_classes=(permissions.AllowAny,)这样用户就不需要用户名和密码来评估文档

from rest_framework import permissions
from drf_yasg import openapi
path('openapi/',get_schema_view(
openapi.Info(
title="School Service",
description="API developers hoping to use our service",
contact=openapi.Contact(email="mail@domain.com"),
license=openapi.License(name="Proprietary Software"),
),
public=True,
permission_classes=(permissions.AllowAny,),
))

最新更新