Django REST Framework Swagger - Authentication Error
<p>I followed the instructions <a href="http://django-rest-swagger.readthedocs.io/en/latest/" rel="nofollow">in the docs</a>. So here's my view:</p>

<pre><code>from rest_framework.decorators import api_view, renderer_classes
from rest_framework import response, schemas
from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer


@api_view()
@renderer_classes([OpenAPIRenderer, SwaggerUIRenderer])
def schema_view(request):
    generator = schemas.SchemaGenerator(title='Bookings API')
    return response.Response(generator.get_schema(request=request))
</code></pre>

<p>And I added the following to my <code>urls.py</code>:</p>

<pre><code>url(r'^docs/', views.schema_view),
</code></pre>

<p>When I went to the <code>/docs/</code> page of my project, I got the following error:</p>

<pre><code>401 : {"detail": "Authentication credentials were not provided."} http://127.0.0.1:8000/docs/?format=openapi
</code></pre>

<p>In the browser console I got this message:</p>

<pre><code>Unable to Load SwaggerUI init.js (line 57)
</code></pre>

<p>When I set the <code>permission_classes</code> of my <code>schema_view</code> to <code>AllowAny</code>, I was able to view my api docs. However, I'm not sure if this is the right way of doing this. Isn't there a way to login as an admin, or any other user to view the docs. Also, how do I provide the auth tokens when viewing this in the browser? Maybe I missed something in the docs.</p>

<p>I think I've found the solution.</p>

<p>In the <code>settings.py</code>, I added the following settings:</p>

<pre><code>SWAGGER_SETTINGS = {
    'SECURITY_DEFINITIONS': {
        'api_key': {
            'type': 'apiKey',
            'in': 'header',
            'name': 'Authorization'
        }
    },
}
</code></pre>

<p>Then when I load the page, I just click on the <em>Authorize</em> button at the upper right and enter this value in the <em>value</em> text field:</p>

<pre><code>Token &lt;valid-token-string&gt;
</code></pre>

<p>However, I still needed to set the permission class of the <code>schema view</code> to <code>AllowAny</code>. The auth token just let me switch from different users, allowing me to view different set of endpoints.</p>

python django django-rest-framework swagger swagger-ui