Django REST框架Swagger - 验证错误 [英] Django REST Framework Swagger - Authentication Error

查看:281
本文介绍了Django REST框架Swagger - 验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照文档中的说明进行操作。所以这里是我的观点:

  from rest_framework.decorators import api_view,renderer_classes 
from rest_framework import response,schemas
从rest_framework_swagger.renderers导入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

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

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

  401:{detail:没有提供身份验证凭证。} http://127.0.0.1:8000/docs/?format=openapi 

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

 无法加载SwaggerUI init.js(第57行)

当我将 schema_view permission_classes 设置为 AllowAny ,我能够查看我的api文档。但是,我不知道这是否是正确的方法。没有办法以管理员或任何其他用户身份登录来查看文档。此外,如何在浏览器中查看此文件时,如何提供授权令牌?也许我错过了文件中的某些东西。

解决方案

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

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

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

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

 令牌< valid-token-string> 

但是,我仍然需要设置模式视图的权限类 AllowAny 。认证令牌只是让我从不同的用户切换,允许我查看不同的端点集。


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:

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

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)

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.

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

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆