0

我使用 volley 库来执行 POST 方法。

我发送了 JsonObjectRequest 的用户名、密码和 grant_type 之类的参数,但我收到以下错误:“错误: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)

}
4

2 回答 2

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 控制台

在此处输入图像描述

于 2018-05-29T04:50:02.400 回答
0

添加:

post.setEntity(new StringEntity("grant_type=password&username=0000@gmail.com&password=00000", "UTF-8"));
于 2018-04-26T11:58:23.497 回答