Milon
Milon

Reputation: 2249

Token response from server with volley to Django rest framework

Server has Django Rest Framework. I have username,password,client_id and client_secret. Now I need to generate access token using these credential. How can I send request to server using (Android)volley so I can get response with token.

(sample response)

{"access_token": "123", "token_type": "Bearer", "expires_in": 36000, "refresh_token": "123", "scope": "read write groups"}

Access token generation process is -

curl -X POST -d "grant_type=password&username=**&password=**" -u "client_id:client_secret" 

url:  http://ip:port/oAuthTokenGeneration/token/

Here what I have tried, but getting com.android.volley.ServerError

  queue = VolleyHandler.getRequestQueue();
  String url="http://ip:port/oAuthTokenGeneration/token/";
    StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            Log.d("access_token:", response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Log.d("error:","Error-------"+ volleyError.toString());

                }
            })
    {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("grant_type","password");
            params.put("username","root");
            params.put("password","root");
            params.put("client_id","id");
            params.put("client_secret", "secret");

            return params;

        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json;");

            return headers;
        }

    };

    queue.add(postRequest);

Upvotes: 2

Views: 729

Answers (1)

See the link. Its a good tut. https://smaspe.github.io/2013/06/06/volley-part2.html

Upvotes: 1

Related Questions