我想在后端使用 Django 和 Django-REST 框架来对本机 android 应用程序上的用户进行身份验证。我目前正在使用基于令牌的身份验证系统。 (更多细节 http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication)
我已经实施了指南中列出的完全相同的过程来设置令牌身份验证。
现在我希望我的用户能够获取令牌以换取凭据。我使用以下代码发出 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());
...
但是,当我将其发布到“views.obtain_auth_token”的 django 后端时,我总是出现此错误:
在服务器上:
"POST /api-token-auth/ HTTP/1.1 400"
我得到的回应:
{"non_field_errors":["Unable to log in with provided credentials."]}
我想了解是什么引发了此 HTTP 400(错误请求错误)