解决 django allauth 中未授权的错误 /rest-auth/registration/

solving error unauthorized /rest-auth/registration/ in django allauth

大家好,我终于可以寻求帮助了,因为我相信我的问题阻碍了我第三天的谷歌搜索。我在前端使用 React,在后台使用 Django,我正在尝试注册用户,登录非常有效!但是当我尝试创建新用户时出现错误

Unauthorized: /rest-auth/registration/
[05/Jun/2019 10:34:45] "POST /rest-auth/registration/ HTTP/1.1" 401 27

我确定这是注册用户的路径,因为当我在浏览器中访问 link 时,它工作正常。问题是我正在从像这样设置的反应前端发送数据

export const authSignUP = (username, email, password1, password2) => {
    return dispatch => {
        dispatch(authStart);
        axios.post('http://127.0.0.1:8000/rest-auth/registration/', {
            username: username,
            email: email,
            password1: password1,
            password2: password2
        }).then(res => {
            const token = res.data.key;
            const expirationDate = new Date(new Date().getTime() + 3600 * 1000);
            localStorage.setItem('token', token);
            localStorage.setItem('expirationDate', expirationDate);
            dispatch(authSuccess(token));
            dispatch(checkAuthTimeOut(3600));
        })
        .catch(err => {
            alert(err)
            // dispatch(authFail(err))
        })
    }
}

我的 django 设置文件是这样的

MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.middleware.common.CommonMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


#This is required otherwise it asks for email server
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'


REST_AUTH_SERIALIZERS = {
    'TOKEN_SERIALIZER': 'jobs_home.serializer.TokenSerializer',
}


ROOT_URLCONF = 'jobs_dj.urls'

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
      os.path.join(BASE_DIR, '../build')
    ],
    'APP_DIRS': True,
    'OPTIONS': {
      'context_processors': [
        'django.template.context_processors.debug',
        'django.template.context_processors.request',
        'django.contrib.auth.context_processors.auth',
        'django.contrib.messages.context_processors.messages',
      ],
    },
  },
]

ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True   
ACCOUNT_USERNAME_REQUIRED = False

#Following is added to enable registration with email instead of username
AUTHENTICATION_BACKENDS = (
 # Needed to login by username in Django admin, regardless of `allauth`
 "django.contrib.auth.backends.ModelBackend",

 # `allauth` specific authentication methods, such as login by e-mail
 "allauth.account.auth_backends.AuthenticationBackend",
)

REST_USE_JWT = True

WSGI_APPLICATION = 'jobs_dj.wsgi.application'

AUTH_PASSWORD_VALIDATORS = [
  {
    'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  },
  {
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  },
  {
    'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  },
  {
    'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  },
]

CORS_ORIGIN_ALLW_ALL = True

CORS_ORIGIN_WHITELIST = (
    'http://localhost:3000', 'http://127.0.0.1:8000',  'http://127.0.0.1:3000'
)

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

我真的会用别人的帮助来打破这堵墙..谢谢

我认为在 settigs.py 文件的 REST_FRAMEWORK 设置中设置身份验证 class 是我将设置更改为

的问题
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
    ),
}

终于成功了。这是我想到的,即使在设置允许任何权限之后,甚至列出数据库对象也需要身份验证。我知道默认情况下它会尝试对用户进行身份验证,即使我不需要它。

我只花了半个小时调试这个所以也许我的解决方案会对某人有所帮助。

我的问题是浏览器仍然有一个令牌并在注册时发送它(由于在本地主机上处理多个项目)。我会仔细检查您的请求 headers.