Django REST HTTP 400获取令牌验证视图时出错 [英] Django REST HTTP 400 Error on Getting Token Authentication View

查看:1183
本文介绍了Django REST HTTP 400获取令牌验证视图时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Django与Django-REST frameowrk在后端来验证Native Android应用程序上的用户。我目前正在使用基于令牌的验证系统。 (更多详情

I want to use Django with Django-REST frameowrk on backend to authenticate users on Native android app. I am currently using Token based auth system. (More details)

我已经执行了与指南列出的完全相同的过程,以设置令牌身份验证。

I have implemented exact same procedure listed by the guide, to setup up the Token Authentication.

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

Now I want my user to be able to obtain token in exchange for credentials. I use make a POST request using following code:

  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());

但是,当我发布到我的django后端views.obtain_auth_token时,我总是这个错误:

However, when I post this to my django backend for "views.obtain_auth_token", I always this error:

在服务器上:

"POST /api-toekn-auth/ HTTP/1.1 400" 

回复我回来了:

{"non_field_errors":["Unable to log in with provided credentials."]}

我想了解什么是扔这个HTTP 400(错误请求错误)

I wish to understand what is throwing this HTTP 400 (Bad Request error)

推荐答案

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

This error appears when the provided login credentials are not valid.

检查以下内容:


  • 您的用户存在于数据库的auth_user表中,is_active字段设置为1?

  • 您的密码正确吗? >
  • 您的用户在authtoken_token表中有一个令牌?

这篇关于Django REST HTTP 400获取令牌验证视图时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆