“error”:“unsupported_grant_type” - 排球

时间:2018-04-26 11:55:53

标签: android-studio kotlin android-volley jsonobjectrequest

我使用排球库来执行POST方法。

我发送了用户名,密码和grant_type之类的JsonObjectRequest参数,但我收到以下错误:“error:unsupported_grant_type”。

试过但却无法真正看出我出错的地方。

我该如何解决?

这是我的代码:

科特林

val url: String = "http://192.168.254.80/MpDS/api/token"
val params = HashMap<String, String>()


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    /*LOGIN BUTTON*/
    BLogin = findViewById(R.id.LoginButton)
    BLogin?.setOnClickListener(this)
}

fun login() {

    params.put("UserName", userTxt.toString())
    params.put("Password", passTxt.toString())
    params.put("grant_type", "password")

    val queue = Volley.newRequestQueue(this@MainActivity)
    val response: String? = null

    val req = JsonObjectRequest(url, JSONObject(params),
            Response.Listener { response ->
                try {
                    VolleyLog.v("Response:%n %s", response.toString(4))
                    textView.text = "work!"
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            }, Response.ErrorListener {
                error -> VolleyLog.e("Error: ", error.message

            )
    })

    fun getHeaders(): Map<String, String> {

        val headers = HashMap<String, String>()
        headers.put("Content-Type", "application/x-www-form-urlencoded")
        return headers
    }

    queue.add(req)

}

2 个答案:

答案 0 :(得分:0)

添加:

post.setEntity(new StringEntity("grant_type=password&username=0000@gmail.com&password=00000", "UTF-8"));

答案 1 :(得分:0)

我无法添加评论,因为我的声誉不允许:(所以这是答案链接:WebAPI call using volley

这是我的android项目中获取令牌的实现(我使用Owin的Asp.net web API)

 public void GetToken(final UserAccount userAccount)
{

    String url = "http://localhost:8081/token";

    StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    // response
                    Log.e("TOKEN_AUTH", response);
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.e("KL", error.networkResponse.toString());
                }
            }
    ) {
        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded";
        }

        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String>  params = new HashMap<String, String> ();
            params.put("grant_type", "password");
            params.put("username", userAccount.getEmail());
            params.put("password", userAccount.getPassword());
            return params;
        }
        @Override
        protected VolleyError parseNetworkError(VolleyError response) {
            try {

                String json = new String(response.networkResponse.data, HttpHeaderParser.parseCharset(response.networkResponse.headers));
                Log.e("KL", "ERROR_RESPONSE = " + json);
            }catch (Exception e){}
            return super.parseNetworkError(response);
        }
    };

    //Add to resquestQueue
    VolleySingleton.getInstance(this).addToRequestQueue(postRequest);
}

Logcat控制台

enter image description here