服务器无法识别 cUrl HttpPost 参数(?)

cUrl HttpPost parameters not recognized(?) by server

我正在尝试从服务器获取授权密钥。在文档中,他们声明我可以通过执行以下 curl 命令检索一个:

$ curl --data "grant_type=authorization_code&code=603372224265" https://gerard2.zportal.nl/api/v2/oauth/token`

我已按照 Whosebug 的说明进行操作并生成了以下内容:

@Override
protected String doInBackground(String... params) {
    mAuthorizationCode = params[0];
    mSchoolCode = params[1];

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("https://" + mSchoolCode + ".zportal.nl/api/v2/oauth/token");
    try{
        List<NameValuePair> nameValuePairs = new ArrayList<>(2);
        httppost.setHeader("Content-Type", "application/json");
        httppost.setHeader("Accept", "application/json");
        nameValuePairs.add(new BasicNameValuePair("grant_type", "authorization_code"));
        nameValuePairs.add(new BasicNameValuePair("code", mAuthorizationCode));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

        HttpParams parameters = httppost.getParams();
        HttpConnectionParams.setConnectionTimeout(parameters, 45000);
        HttpConnectionParams.setSoTimeout(parameters, 45000);

        HttpResponse response = httpClient.execute(httppost);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line;
        while((line = reader.readLine()) != null){
            Log.e(LOG_TAG, line);
        }
    }catch(ClientProtocolException e){
        Log.e(LOG_TAG, "Error", e);
    }catch(IOException e){
        Log.e(LOG_TAG, "IO Error", e);
    }
    return null;
}

服务器发送给我的响应是:

    E/ScheduleRetriever: {"response":{"status":500,"message":"Intern probleem op het portal.","details":"class org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'grant_type' is not present","eventId":767369,"startRow":0,"endRow":0,"totalRows":0,"data":[]}

虽然我确实包含了 POST_parameter "grant_type"。谁能帮我弄清楚为什么服务器说它没有收到参数 grant_type?

谢谢。

*编辑:我使用的代码显然已过期。生成了一个新的,它完全有效。谢谢!

您不应该使用行 httppost.setHeader("Content-Type", "application/json");

覆盖对 application/json 的 POST 请求的 Content-Type

这很可能导致服务器误解数据,因为请求的 Content-type 应该是 application/x-www-form-urlencoded

删除以下行,它应该可以工作:

httppost.setHeader("Content-Type", "application/json");