【问题标题】:grant type not supported error unsupported_grant_type in android?android 中的授权类型不支持错误 unsupported_grant_type?
【发布时间】:2015-10-07 10:55:57
【问题描述】:

我正在尝试在发布请求中使用参数调用服务,我的参数是 grant_type="password"、username="XXXXXX"、password="yyyyyyy" 并且我得到了像 {"error":"unsupported_grant_type"} 这样的异常

我在这里发送如下请求

String uri = Uri.parse(ServiceUrl.LOGIN_SERVER_URL)
                        .buildUpon()
                        .appendQueryParameter("grant_type","password")
                        .appendQueryParameter("username",userName.getText().toString().trim())
                        .appendQueryParameter("password", urlEncoded)
                        .build().toString();

我的调用服务代码以获取服务响应:

@SuppressWarnings("deprecation")
    public static String httpPostQueryParamService(String Url) {

        String serveresponse = null;
        try {

        HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams,
                    ServiceUrl.timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT)
            // in milliseconds which is the timeout for waiting for data.
            HttpConnectionParams.setSoTimeout(httpParams,
                    ServiceUrl.timeoutSocket);
            HttpClient client = new DefaultHttpClient(httpParams);
            HttpPost request = new HttpPost(Url);

        request.addHeader("Content-Type","application/x-www-form-urlencoded");


            HttpResponse response = client.execute(request);
            HttpEntity responseEntity = response.getEntity();
            serveresponse = EntityUtils.toString(responseEntity);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_BAD_GATEWAY) {

                serveresponse = null;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return serveresponse;
        }

【问题讨论】:

  • 那东西不应该像grant_type=password&username=username&password=password一样进入身体吗?
  • 检查我的答案,我不能确定,但​​是当我这样做时,我必须将其添加为正文。

标签: android web-services


【解决方案1】:

我相信你需要像这样将这些东西添加到方法体中:

urlConnection.setDoOutput(true); 

// Add the body
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
wr.writeBytes("&grant_type=password&username=username&password=password");
wr.flush(); 
wr.close();

【讨论】:

  • 你能给我发送完整的代码如何将帖子参数发送到服务吗?我是安卓新手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 2019-05-21
相关资源
最近更新 更多