[Fixed]-Authenticating Swagger API docs (drf-yasg)

13👍

Turned out the issue was that I was missing rest_framework.authentication.SessionAuthentication in DRF’s DEFAULT_AUTHENTICATION_CLASSES.

So the request object sent after logging in via the Django Admin login view did not have a user so all permission classes kept failing and it would then lead to the above error.

So after adding it drf-yasg shows all its glory.

Bug:

The error it throws when this occurs was raised as bug though. See #issue58.

👤lukik

0👍

you should set the authentication class in schema view if you only use JWT:

REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework_simplejwt.authentication.JWTAuthentication",
    ],
}

so your schema view should be something like that:

  schema_view = get_schema_view(
      openapi.Info(
        title="Swagger Doc",
        default_version='v1',
        description="Test description",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="contact@snippets.local"),
        license=openapi.License(name="BSD License"),
      ),
    authentication_classes=(authentication.BasicAuthentication,),
    public=True,
  )

it should work and ask you to set your username and password.

Leave a comment