gpt4 book ai didi

Django:Rest Framework 验证 header

转载 作者:行者123 更新时间:2023-11-28 19:34:55 25 4
gpt4 key购买 nike

使用 Django REST API,我正在尝试验证我的请求。

这是我要发送的内容:

Content-Type: application/json, Authentication: token="6d82549b48a8b079f618ee9c51a6dfb59c7e2196"

这是我得到的结果:

{"detail": "Authentication credentials were not provided."}

谁能给我正确的标题?

谢谢

标题:

Accept: application/json
Content-Type: application/json
Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17

enter image description here

设置.py

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.permissions.IsAdminUser',


),
'PAGINATE_BY': 10
}

View .py

class ProfileList(generics.ListCreateAPIView):
"""
API endpoint that represents a list of users.
"""
permission_classes = (permissions.IsAuthenticated,)
model = Profile
serializer_class = ProfileSerializer

def pre_save(self, obj):
obj.owner = self.request.user

最佳答案

以防其他人遇到此错误。如果您使用 mod_wsgi 在 Apache 上运行 Django,也会发生这种情况,因为授权 header 已被 mod_wsgi 剥离。您需要将以下内容添加到您的 VirtualHost 配置中:

WSGIPassAuthorization 开启

关于Django:Rest Framework 验证 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14877249/

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