Home > Blockchain >  rest_framework_swagger installation - HTTP 401 Unauthorized
rest_framework_swagger installation - HTTP 401 Unauthorized

Time:12-02

I have installed rest_framework_swagger successfully, after that I set url in urls.py file project:

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')

]

but if I browse http://172.18.0.1:7000/openapi/ I get this:

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."
}

Why do I get "Authentication credentials were not provided"? It should show me schema of all apis not to test they.

CodePudding user response:

Try using permission_classes=(permissions.AllowAny,) to your code then the user not needed a username and password to assess your documentation

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="[email protected]"),
        license=openapi.License(name="Proprietary Software"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
))
  • Related