Django REST框架Swagger - 身份验证错误

时间:2022-06-30 03:15:48

I followed the instructions in the docs. So here's my view:

我按照文档中的说明进行操作。所以这是我的看法:

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

And I added the following to my urls.py:

我在urls.py中添加了以下内容:

url(r'^docs/', views.schema_view),

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

当我转到项目的/ docs /页面时,出现以下错误:

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

In the browser console I got this message:

在浏览器控制台中,我收到了以下消息:

Unable to Load SwaggerUI init.js (line 57)

When I set the permission_classes of my schema_view to AllowAny, 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.

当我将schema_view的permission_classes设置为AllowAny时,我能够查看我的api文档。但是,我不确定这是否是正确的方法。是否有办法以管理员或任何其他用户身份登录以查看文档。另外,在浏览器中查看时如何提供身份验证令牌?也许我在文档中遗漏了一些东西。

2 个解决方案

#1


11  

I think I've found the solution.

我想我找到了解决方案。

In the settings.py, I added the following settings:

在settings.py中,我添加了以下设置:

SWAGGER_SETTINGS = {
    'SECURITY_DEFINITIONS': {
        'api_key': {
            'type': 'apiKey',
            'in': 'header',
            'name': 'Authorization'
        }
    },
}

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

然后当我加载页面时,我只需单击右上角的“授权”按钮,然后在值文本字段中输入此值:

Token <valid-token-string>

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

但是,我仍然需要将架构视图的权限类设置为AllowAny。身份验证令牌让我从不同的用户切换,允许我查看不同的端点集。

#2


1  

Isn't there a way to login as an admin, or any other user to view the docs.

是否有办法以管理员或任何其他用户身份登录以查看文档。

If your only use token authentication, first create tokens for your users, then access the resources by setting the header

如果您仅使用令牌身份验证,请首先为您的用户创建令牌,然后通过设置标头来访问资源

curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'

#1


11  

I think I've found the solution.

我想我找到了解决方案。

In the settings.py, I added the following settings:

在settings.py中,我添加了以下设置:

SWAGGER_SETTINGS = {
    'SECURITY_DEFINITIONS': {
        'api_key': {
            'type': 'apiKey',
            'in': 'header',
            'name': 'Authorization'
        }
    },
}

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

然后当我加载页面时,我只需单击右上角的“授权”按钮,然后在值文本字段中输入此值:

Token <valid-token-string>

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

但是,我仍然需要将架构视图的权限类设置为AllowAny。身份验证令牌让我从不同的用户切换,允许我查看不同的端点集。

#2


1  

Isn't there a way to login as an admin, or any other user to view the docs.

是否有办法以管理员或任何其他用户身份登录以查看文档。

If your only use token authentication, first create tokens for your users, then access the resources by setting the header

如果您仅使用令牌身份验证,请首先为您的用户创建令牌,然后通过设置标头来访问资源

curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'