服务器具有Django Rest Framework。我有用户名,密码,client_id和client_secret。现在,我需要使用这些凭据生成访问令牌。如何使用(Android)齐射向服务器发送请求,以便获得令牌响应。

(样本响应)

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


访问令牌生成过程为-

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

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


这是我尝试过的但是得到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);

最佳答案

请参阅链接。很好。
https://smaspe.github.io/2013/06/06/volley-part2.html

关于android - 带有Volley的服务器对Django Rest框架的 token 响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36631525/

10-12 04:36