我正在使用Django Rest Framework来构建API . 我正在使用令牌认证 . 我被要求获得有效的令牌 . 然后我调用另一个API点来请求数据并发送我的令牌 . 我似乎没有进行身份验证 . 当我调试打印到服务器时,我的用户是Annonymous,而我的request.auth是None .
我在VueJS中使用Javascript来拨打电话:
fetchHabits: function() {
console.log('Token '+this.authToken.token);
fetch('http://127.0.0.1:8000/api/habit/?format=json',{
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Token '+this.authToken.token
},
})
.then(response => response.json())
.then(json => this.habits = json)
}
任何人都可以告诉我如何将有效的令牌传递给DRF以便识别它 . 如果我必须在一个类中编写代码,那么有效的令牌存储在哪里?
谢谢
这是我的身份验证类 . 请注意两个注释掉,除非他们注释掉api发送用户名/密码不起作用:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
# 'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.TokenAuthentication',
), }
效果:目标是发送用户名/密码获取令牌,然后使用该令牌执行请求 .
'DEFAULT_AUTHENTICATION_CLASSES': (
# 'rest_framework.authentication.BasicAuthentication',
# 'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.TokenAuthentication',
获取令牌,500错误匿名用户(第二部分失败)
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
# 'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.TokenAuthentication',
获取令牌,500错误匿名用户
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.TokenAuthentication',
获取令牌,500错误匿名用户
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
401未经授权
在@Rajan的帮助下,我发现Header有一个未知的令牌(不在我的数据库中)并返回匿名用户,如果我强制令牌内联它可以工作 . 但是当前的头部认证似乎不是用户在头部中传递的令牌而是另一个 . 是否可以在此过程中进行修改?
1 回答
解决方案是HTTP标头'损坏'我需要把凭证包括在内 . 最终解决方案是: