Search code examples
androidasp.net-web-apiandroid-volleytoken

Android Volley .NET Rest token authorization fails


I am trying to use web API's I built in .NET Framework that uses a token authentication system. These API's are being consumed in Android using Volley.

So these API's work perfectly when tested in postman, so I'm going to share a screenshot of the postman configuration following the code I'm using in Android.

Token auth postman

public static void authorizeAPIClient(final String username, final String password, RequestQueue requestQueue){

    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            apiPath + "api/authorise/token",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    MainActivity.getToken(response);
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {


        }
    }) {

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded; charset=UTF-8";
        }



        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("grant_type", "password");
            params.put("username", username);
            params.put("password", password);
            return params;
        }

    };

    requestQueue.add(stringRequest);
}

So this part works perfectly and I am able to extract the token from the JSON and even tested the token received on the Android device using postman and it works perfectly. What I find weird about this is that I use getParams() to pass it the username and password while in postman I need to do it via a form.

Now that we have gotten a token from the server and established that it works I try and call another function that requires a token.

Get User Details

public static void getUserDetails(final String userId, final String token, RequestQueue requestQueue){

    StringRequest stringRequest = new StringRequest(Request.Method.GET,
            apiPath + "API/points/getUserDetails",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.i("API", response);
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("API", error.toString());

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("UserId", userId);
            return params;
        }
        @Override
        public Map<String, String> getHeaders(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("Authorization", "Bearer " + token);
            return params;
        }
    };

    requestQueue.add(stringRequest);

When calling this function I get one of the following errors depending on how I'm passing params and headers:

  1. com.android.volley.ServerError

  2. com.android.volley.AuthFailureError

I'm not sure which error comes first, ie, can it throw a server error before or after an AuthFailureError, so it's difficult to find out if it ever authorizes or if the URL params are passed correctly.

Does anyone know what I'm doing wrong, I've been scouring through StackOverFlow and nothing has worked for me.

Also, sorry about all the black lining, sensitive information :D


Solution

  • Try this

    public static void getUserDetails(final String userId, final String token, RequestQueue requestQueue){
    
    StringRequest stringRequest = new StringRequest(Request.Method.GET,
            apiPath + "API/points/getUserDetails?UserId=" + userId,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
    
                    Log.i("API", response);
                }
            }, new Response.ErrorListener() {
    
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("API", error.toString());
    
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError{
            Map<String, String> params = new HashMap<String, String>();
            params.put("Authorization", "Bearer " + token);
            return params;
        }
    };
    
    requestQueue.add(stringRequest);