【问题标题】:"error":"unsupported_grant_type" - Volley“错误”:“unsupported_grant_type” - 排球
【发布时间】:2018-10-07 02:48:30
【问题描述】:

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

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

}

【问题讨论】:

    标签: android-studio kotlin android-volley jsonobjectrequest


    【解决方案1】:

    添加:

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

    【讨论】:

    • 感谢您的回答,但这并不能解决问题,因为这是与 httpClient 一起使用的,而我目前正在使用 volley。可能是类似的东西吗?
    【解决方案2】:

    我无法添加 cmets,因为我的声誉不允许 :( 所以这是答案链接: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 控制台

    【讨论】:

    • 是一个回答者,编辑:哦,不,他使用 kotlin 并且代码是 Java :(
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 2017-12-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多