我正在尝试使用axios从/ rest-auth / user /页面获取信息。这是我的功能:
export const fetchUser = () => {
const token = localStorage.getItem('token');
return dispatch => {
dispatch(fetchUserPending());
axios.get('http://localhost:8000/api/v1/rest-auth/user/', {headers: { 'authorization': `Bearer ${token}`}})
.then(response => {
const user = response.data;
dispatch(fetchUserFulfilled(user));
})
.catch(err => {
dispatch(fetchUserRejected(err));
})
}
}
它使用我从登录名获得的django令牌,该令牌存储在localStorage中。我收到错误状态403,未提供身份验证凭据。我尝试编辑django settings.py文件以使其包含
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
],
}
,然后我得到未授权的错误代码401。谁能指引我正确的方向?谢谢!
答案 0 :(得分:3)
默认情况下,rest_framework.authentication.TokenAuthentication
使用关键字Token
而不是Bearer
。呼叫应为:
axios.get('http://localhost:8000/api/v1/rest-auth/user/', {headers: { 'Authorization': `Token ${token}`}})