python - djangorest框架授权返回错误的状态代码

标签 python django rest django-rest-framework http-status-codes

我是 Django 新手。我有一个项目,手机可以使用 token 与服务器进行交互。在 settings.py 中我有:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'UPLOADED_FILES_USE_URL': False,

    'DEFAULT_PAGINATION_CLASS': None,
    'PAGE_SIZE': DEFAULT_PAGE_SIZE,  # For views using PageNumberPagination
}

但是当使用 postman 时,我发送带有无效 token 的请求,而不是 401(未经授权),而是返回 403(禁止)。我可以采取什么特殊措施来解决这个问题吗?

tnx

最佳答案

正如documentation所述:

The kind of response that will be used depends on the authentication scheme. Although multiple authentication schemes may be in use, only one scheme may be used to determine the type of response. The first authentication class set on the view is used when determining the type of response.

添加来自 SessionAuthentication 的摘录

Unauthenticated responses that are denied permission will result in an HTTP 403 Forbidden response.

你已经有了答案。

TokenAuthentication 移至第一个 DEFAULT_AUTHENTICATION_CLASSES 或记录当前行为。

关于python - djangorest框架授权返回错误的状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48656995/

相关文章:

python - pandas 中的 groupby 具有必须保持状态的功能

python - Pygame - 如何均匀地分布随机生成的平台?

Django 1.4 在开发服务器上提供 MEDIA_URL 和 STATIC_URL 文件

rest - JIRA REST API -- 如何查询问题状态名称

python - 将一个字典的键替换为另一个字典的值以创建一个新字典

在 Gnome Shell 中带有通知的 Python 程序不起作用

python - Celery 4 不自动发现任务

python - tastypie 过滤外键相关资源

php - 面向对象的 PHP 中的非阻塞 HTTP 请求?

web-services - WS 被认为是 Restful 要求