1. <i id='ZBqbS'><tr id='ZBqbS'><dt id='ZBqbS'><q id='ZBqbS'><span id='ZBqbS'><b id='ZBqbS'><form id='ZBqbS'><ins id='ZBqbS'></ins><ul id='ZBqbS'></ul><sub id='ZBqbS'></sub></form><legend id='ZBqbS'></legend><bdo id='ZBqbS'><pre id='ZBqbS'><center id='ZBqbS'></center></pre></bdo></b><th id='ZBqbS'></th></span></q></dt></tr></i><div id='ZBqbS'><tfoot id='ZBqbS'></tfoot><dl id='ZBqbS'><fieldset id='ZBqbS'></fieldset></dl></div>
      • <bdo id='ZBqbS'></bdo><ul id='ZBqbS'></ul>

      <legend id='ZBqbS'><style id='ZBqbS'><dir id='ZBqbS'><q id='ZBqbS'></q></dir></style></legend>

    1. <small id='ZBqbS'></small><noframes id='ZBqbS'>

    2. <tfoot id='ZBqbS'></tfoot>
    3. Django REST Framework Swagger - 身份验证错误

      时间:2023-09-28
        <bdo id='fD9Zv'></bdo><ul id='fD9Zv'></ul>
      • <small id='fD9Zv'></small><noframes id='fD9Zv'>

        <tfoot id='fD9Zv'></tfoot>

                <legend id='fD9Zv'><style id='fD9Zv'><dir id='fD9Zv'><q id='fD9Zv'></q></dir></style></legend>
                <i id='fD9Zv'><tr id='fD9Zv'><dt id='fD9Zv'><q id='fD9Zv'><span id='fD9Zv'><b id='fD9Zv'><form id='fD9Zv'><ins id='fD9Zv'></ins><ul id='fD9Zv'></ul><sub id='fD9Zv'></sub></form><legend id='fD9Zv'></legend><bdo id='fD9Zv'><pre id='fD9Zv'><center id='fD9Zv'></center></pre></bdo></b><th id='fD9Zv'></th></span></q></dt></tr></i><div id='fD9Zv'><tfoot id='fD9Zv'></tfoot><dl id='fD9Zv'><fieldset id='fD9Zv'></fieldset></dl></div>

                  <tbody id='fD9Zv'></tbody>
              • 本文介绍了Django REST Framework Swagger - 身份验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

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

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

                And I added the following to my urls.py:

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

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

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

                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)
                

                当我将 schema_viewpermission_classes 设置为 AllowAny 时,我能够查看我的 api 文档.但是,我不确定这是否是正确的方法.没有办法以管理员或任何其他用户身份登录以查看文档.另外,在浏览器中查看时如何提供身份验证令牌?也许我错过了文档中的某些内容.

                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.

                推荐答案

                我想我已经找到了解决方案.

                I think I've found the solution.

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

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

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

                然后当我加载页面时,我只需点击右上角的 Authorize 按钮,然后在 value 文本字段中输入此值:

                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>
                

                但是,我仍然需要将 schema view 的权限类设置为 AllowAny.auth token 只是让我从不同的用户切换,允许我查看不同的端点集.

                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.

                这篇关于Django REST Framework Swagger - 身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何配置“HTTPS"?带有 drf-yasg 自动生成的招摇页面的方案? 下一篇:Django REST 框架 + Django REST Swagger + ImageField

                相关文章

                    <bdo id='EP4kN'></bdo><ul id='EP4kN'></ul>
                    <tfoot id='EP4kN'></tfoot>

                    <legend id='EP4kN'><style id='EP4kN'><dir id='EP4kN'><q id='EP4kN'></q></dir></style></legend>
                    <i id='EP4kN'><tr id='EP4kN'><dt id='EP4kN'><q id='EP4kN'><span id='EP4kN'><b id='EP4kN'><form id='EP4kN'><ins id='EP4kN'></ins><ul id='EP4kN'></ul><sub id='EP4kN'></sub></form><legend id='EP4kN'></legend><bdo id='EP4kN'><pre id='EP4kN'><center id='EP4kN'></center></pre></bdo></b><th id='EP4kN'></th></span></q></dt></tr></i><div id='EP4kN'><tfoot id='EP4kN'></tfoot><dl id='EP4kN'><fieldset id='EP4kN'></fieldset></dl></div>

                  1. <small id='EP4kN'></small><noframes id='EP4kN'>