error response with Json request to Disqus on Volley (Android)

961 Views Asked by At

I am trying to connectt to Disqus with their api

(Specifically I am calling to POST https://disqus.com/api/oauth/2.0/access_token/ )

I am using JsonObjectRequest with Volley library for the network calls. My response is consistently an error with code 400:

11-19 12:48:17.119: E/Volley(16124): [107902] BasicNetwork.performRequest: Unexpected response code 400 for http://disqus.com/api/oauth/2.0/access_token/

I have tried to proxy the request in Charles to see more info, and got this as the error message:

enter image description here

And this is my request as it was recorded by Charles (As you can see I did add the parameter grant_type):

enter image description here

I am calling the same request in the iOS version of my app, and am using the same keys and information, and everything works there. This is why I assume the problem is somewhere in my code, perhaps in the way I add/encode/not encode my parameters... This is the code I use to send the request:

    String url = "http://disqus.com/api/oauth/2.0/access_token/";

    JSONObject jsonObj = new JSONObject();
    try {
        jsonObj.put("grant_type", "authorization_code");
        jsonObj.put("client_id", publicKey());
        jsonObj.put("client_secret", SecretKey());
        jsonObj.put("redirect_uri", redirectUrl());
        jsonObj.put("code", code);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            url, jsonObj, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try {
                        String refreshToekn = (String) response.get("refresh_token");
                        String accessToekn = (String) response.get("access_token");

                        String stam = refreshToekn+accessToekn;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("Error: " + error.getMessage());
                }
            });

    VolleyController.getInstance().addToRequestQueue(jsonObjReq);

The call always falls into onErrorResponse

I have also tried to use StringRequest and add the parameters in the url with no successes

1

There are 1 best solutions below

0
On

So I managed to get it to work, but I would love if someone could explain why my solution works. Basically, instead of using JSONObject and passing it to JsonObjectRequest I used a StringRequest and overrode getParams.

Was I wrong trying to send a POST request with JsonObjectRequest?

This is the entire solution:

String url = "http://disqus.com/api/oauth/2.0/access_token/";
    StringRequest stringReq = new StringRequest(Method.POST,
            url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    String string = response;
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("error: " + error.getMessage());
                }
            }) {
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();

            params.put("grant_type", "authorization_code");
            params.put("client_id", publicKey());
            params.put("client_secret", SecretKey());
            params.put("redirect_uri",redirectUrl());
            params.put("code", code);

            return params;
        }

        @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");
            return params;
        }
    };

    VolleyController.getInstance().addToRequestQueue(stringReq);