如何在django rest框架中从电子邮件和密码中获取身份验证令牌?

时间:2017-06-30 08:39:07

标签: android python rest authentication django-rest-auth

我有一个django rest api作为我的android应用程序的后端。我希望我的应用用户能够登录并注册我的应用。当用户注册或将新用户添加到用户表时,应生成该用户的身份验证令牌。我使用用户模型中的以下代码执行此操作:

# This code is triggered whenever a new user has been created and saved to the database
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

现在,当我尝试以新创建的用户身份登录时,使用令牌身份验证时,我需要做的就是在用户请求正文中发送电子邮件和密码。我这样做是使用改造2:

public interface UserService {
    @POST("users/api-token-auth/")
    Call<String> loginInToken(@Body LoginCredentials loginCredentials);
}

LoginCredentials类看起来像这样:

public class LoginCredentials {

    private String email;
    private String password;

    public LoginCredentials() { }

    public LoginCredentials(String email, String password) {
        this.email = email;
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }
}

在我的应用程序中,我使用UserService中包含的此接口方法对django rest api进行以下调用:

@Override
public void loginEmailUser(LoginCredentials loginCredentials) {
    Call<String> call = userServiceApi.loginInToken(loginCredentials);
    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            Log.d("USER_REPOSITORY", response.toString());
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Log.d("USER_REPOSITORY", t.toString());
        }
    });
}

如果成功,电子邮件和密码已经张贴到后端以换取相应用户的身份验证令牌,因此我应该通过发出此请求来接收令牌。但是,当调用此端点api-token-auth时,使用以下throwable调用onFailure方法:

USER_REPOSITORY: Response{protocol=http/1.0, code=400, message=Bad Request, url=http://XXX.YYY.Z.AAA:8000/users/api-token-auth/}

这是我的django urls.py文件,它对应于来自android客户端的被调用的url:

from django.conf.urls import url
from users import views as user_views
from rest_framework.authtoken import views as auth_views

urlpatterns = [
    url(r'^api-token-auth/', auth_views.obtain_auth_token),
    url(r'^create/', user_views.UserCreate.as_view(), name="create"),
    url(r'^$', user_views.UserList.as_view(), name="users_list"),
    url(r'^(?P<pk>[0-9]+)/$', user_views.UserDetail.as_view(), name="user_detail"),
]

django rest docs说,使用POST的电子邮件和密码调用api-token-auth url会导致返回令牌并且状态代码为200.

为什么我似乎按照成功请求的指示做了错误的请求和状态代码400?

1 个答案:

答案 0 :(得分:0)

我正在使用OAUth添加示例 LOGIN类。我正在使用Volley库

public class Login extends AppCompatActivity implements View.OnClickListener {

    EditText userName, Password;
    Button login;
    public static final String LOGIN_URL = "http://192.168.100.5:84/Token";
    public static final String KEY_USERNAME = "UserName";
    public static final String KEY_PASSWORD = "Password";
    String username, password;
    String accesstoken, tokentype, expiresin, masterid, name, access, issue, expires, masterid1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userName = (EditText) findViewById(R.id.login_name);
        Password = (EditText) findViewById(R.id.login_password);
        userName.setHint(Html.fromHtml("<font color='#008b8b' style='italic'>Username</font>"));
        Password.setHint(Html.fromHtml("<font color='#008b8b'>Password</font>"));
        login = (Button) findViewById(R.id.login);
        login.setOnClickListener(this);
    }

    private void UserLogin() {

        username = userName.getText().toString().trim();
        password = Password.getText().toString().trim();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            accesstoken = jsonObject.getString("access_token");
                            tokentype = jsonObject.getString("token_type");
                            expiresin = jsonObject.getString("expires_in");
                            username = jsonObject.getString("userName");
                            masterid = jsonObject.getString("MasterID");
                            masterid = masterid.replaceAll("[^\\.0123456789]", "");

                            masterid1 = jsonObject.getString("MasterID");

                            name = jsonObject.getString("Name");
                            access = jsonObject.getString("Access");
                            issue = jsonObject.getString(".issued");
                            expires = jsonObject.getString(".expires");
                            SessionManagement session = new SessionManagement(Login.this);
                            session.createLoginSession(accesstoken, tokentype, expiresin, username, masterid, name, access, issue, expires);
                            // session.createLoginSession(masterid1);
                            openProfile();

                        } catch (JSONException e) {
                            Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show();
                        Toast.makeText(Login.this, "Please enter valid username and Password", Toast.LENGTH_SHORT).show();
                    }
                }) {


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                //params.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
                return params;
            }

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> map = new HashMap<String, String>();
                map.put(KEY_USERNAME, username);
                map.put(KEY_PASSWORD, password);
                //map.put("access_token", accesstoken);
                map.put("grant_type", "password");
                return map;
            }
        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }


    private void openProfile() {
        Intent intent = new Intent(this, Home.class);
        intent.putExtra(KEY_USERNAME, username);
        startActivity(intent);


        startActivity(intent);

    }

    @Override
    public void onClick(View v) {
        UserLogin();
    }


}
  

这是Sample .please将其转换为您的要求