我正在使用这两个很棒的工具 DRF和 Django-REST-Swagger ,但是我的一些 API View 处于 token 身份验证之下。
所以现在我想在我的 API 的 swagger doc 页面中添加测试那些 token 身份验证 api url 的可能性,包括 token header 。我怎么能这样做?
我的类 API View 的快照如下所示:
class BookList(APIView):
"""
List all books, or create a new book.
"""
authentication_classes = (TokenAuthentication, )
permission_classes = (IsAuthenticated,)
...
由于 Swagger 自动检测到很多东西,我希望注意到 token 身份验证,并在其 Web 界面中询问我有关 token 或用户 ID 的信息,但事实并非如此。因此我正在通过 CURL 命令手动测试它......
最佳答案
如果您使用 token 身份验证,您可能需要查看 this question
基本上,您只需要将此添加到您的 settings.py
:
SWAGGER_SETTINGS = {
'SECURITY_DEFINITIONS': {
'api_key': {
'type': 'apiKey',
'in': 'header',
'name': 'Authorization'
}
},
}
在您的 Swagger UI 页面中,您应该会看到一个授权按钮。单击它并在输入文本字段中输入您的授权值。
关于django - 如何将 token 身份验证添加到 swagger + django rest 框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24864902/