gpt4 book ai didi

Django REST Framework 身份验证关键字

转载 作者:行者123 更新时间:2023-12-02 19:49:02 26 4
gpt4 key购买 nike

我正在尝试将 Rest_framework TokenAuthentication 关键字从“Token”重命名为“Bearer”,如文档中的建议,我已将 TokenAuthentication 类子类化,如下所示:

模块中:user/authentication.py

from rest_framework import authentication

class TokenAuthentication(authentication.TokenAuthentication):
"""
Simple token based authentication.
Clients should authenticate by passing the token key in the "Authorization"
HTTP header, prepended with the string "Token ". For example:
Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
"""

keyword = 'Bearer'

在模块 app/settings.py 中

 REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'user.authentication.TokenAuthentication',
),
}

当我使用“授权:持有者... token ...”但不使用“授权: token ... token ...”时,它仍然向我发送 401 未经授权

我做错了什么?

最佳答案

从rest_framework导入身份验证

类 TokenAuthentication(authentication.TokenAuthentication):

authentication.TokenAuthentication.keyword = 'Bearer'

关于Django REST Framework 身份验证关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58648452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com