Django REST Framework Swagger - 身份验证错误
Django REST Framework Swagger - Authentication Error
我遵循了说明 in the docs。所以这是我的观点:
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
中添加了以下内容:
url(r'^docs/', views.schema_view),
当我进入项目的 /docs/
页面时,出现以下错误:
401 : {"detail": "Authentication credentials were not provided."} http://127.0.0.1:8000/docs/?format=openapi
在浏览器控制台中我收到这条消息:
Unable to Load SwaggerUI init.js (line 57)
当我将 schema_view
的 permission_classes
设置为 AllowAny
时,我能够查看我的 api 文档。但是,我不确定这是否是正确的做法。有没有办法以管理员或任何其他用户身份登录来查看文档。另外,在浏览器中查看时如何提供身份验证令牌?也许我在文档中遗漏了一些东西。
Isn't there a way to login as an admin, or any other user to view the docs.
如果你只使用token认证,先create tokens for your users,然后通过设置header访问资源
curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'
我想我找到了解决办法。
在settings.py
中,我添加了以下设置:
SWAGGER_SETTINGS = {
'SECURITY_DEFINITIONS': {
'api_key': {
'type': 'apiKey',
'in': 'header',
'name': 'Authorization'
}
},
}
然后当我加载页面时,我只需点击右上角的 Authorize 按钮并在 value 中输入此值文本字段:
Token <valid-token-string>
但是,我仍然需要将 schema view
的权限 class 设置为 AllowAny
。授权令牌让我可以从不同的用户切换,让我可以查看不同的端点集。
我遵循了说明 in the docs。所以这是我的观点:
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
中添加了以下内容:
url(r'^docs/', views.schema_view),
当我进入项目的 /docs/
页面时,出现以下错误:
401 : {"detail": "Authentication credentials were not provided."} http://127.0.0.1:8000/docs/?format=openapi
在浏览器控制台中我收到这条消息:
Unable to Load SwaggerUI init.js (line 57)
当我将 schema_view
的 permission_classes
设置为 AllowAny
时,我能够查看我的 api 文档。但是,我不确定这是否是正确的做法。有没有办法以管理员或任何其他用户身份登录来查看文档。另外,在浏览器中查看时如何提供身份验证令牌?也许我在文档中遗漏了一些东西。
Isn't there a way to login as an admin, or any other user to view the docs.
如果你只使用token认证,先create tokens for your users,然后通过设置header访问资源
curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'
我想我找到了解决办法。
在settings.py
中,我添加了以下设置:
SWAGGER_SETTINGS = {
'SECURITY_DEFINITIONS': {
'api_key': {
'type': 'apiKey',
'in': 'header',
'name': 'Authorization'
}
},
}
然后当我加载页面时,我只需点击右上角的 Authorize 按钮并在 value 中输入此值文本字段:
Token <valid-token-string>
但是,我仍然需要将 schema view
的权限 class 设置为 AllowAny
。授权令牌让我可以从不同的用户切换,让我可以查看不同的端点集。