获取令牌身份验证视图时出现 Django REST HTTP 400 错误

sil*_*ave 5 django android django-authentication django-rest-framework

我想在后端使用 Django 和 Django-REST 框架来对本机 android 应用程序上的用户进行身份验证。我目前正在使用基于令牌的身份验证系统。(更多细节

我已经实施了指南中列出的完全相同的过程来设置令牌身份验证。

现在我希望我的用户能够获取令牌以换取凭据。我使用以下代码发出 POST 请求:

      JSONObject cred = new JSONObject();

            try {
                cred.put("password",mPassword);
                cred.put("username",mEmail);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            try {
               
                HttpClient httpClient = new DefaultHttpClient();

                HttpPost httpPost = new HttpPost(Common.getServerUrl()+"/api-token-auth/");
                StringEntity credentials = new StringEntity( cred.toString());
                credentials.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");
                httpPost.setEntity(credentials);
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();

                // Read content & Log
                inputStream = httpEntity.getContent();
                Log.i("Asynctask", cred .toString());
...
Run Code Online (Sandbox Code Playgroud)

但是,当我将其发布到“views.obtain_auth_token”的 django 后端时,我总是出现此错误:

在服务器上:

"POST /api-token-auth/ HTTP/1.1 400" 
Run Code Online (Sandbox Code Playgroud)

我得到的回应:

{"non_field_errors":["Unable to log in with provided credentials."]}
Run Code Online (Sandbox Code Playgroud)

我想了解是什么引发了此 HTTP 400(错误请求错误)

sc3*_*c3w 4

当提供的登录凭据无效时,会出现此错误。

检查以下内容:

  • 您的用户存在于数据库的 auth_user 表中并且 is_active 字段设置为 1?
  • 您的密码正确吗?
  • 您的用户在 authtoken_token 表中有一个令牌?