我正在使用OAuth-Signpost
Java库来签署从客户端发送到实现OAuth身份验证的服务器的请求.当GET请求(使用HttpURLConnection)时,一切都正常:请求被签名,参数被包括,签名在目的地匹配.但是,它似乎不适用于POST请求.我知道在使用HttpURLConnection签名POST时可能出现的问题,所以我转移到Apache HttpComponents库以获得这些请求.在以下示例中发送的参数是纯字符串和类似XML的字符串(“rxml”).我的代码如下:
public Response exampleMethod(String user,String sp,String ep,String rn,String rxml){
//All these variables are proved to be correct (they work right in GET requests)
String uri = "...";
String consumerKey = "...";
String consumerSecret = "...";
String token = "...";
String secret = "...";
//create the parameters list
List params = new ArrayList();
params.add(new BasicNameValuePair("user",user));
params.add(new BasicNameValuePair("sp",sp));
params.add(new BasicNameValuePair("ep",ep));
params.add(new BasicNameValuePair("rn",rn));
params.add(new BasicNameValuePair("rxml",rxml));
// create a consumer object and configure it with the access
// token and token secret obtained from the service provider
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
consumer.setTokenWithSecret(token,secret);
// create an HTTP request to a protected resource
HttpPost request = new HttpPost(uri);
// sign the request
consumer.sign(request);
// set the parameters into the request
request.setEntity(new UrlEncodedFormEntity(params));
// send the request
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
//if request was unsuccessful
if(response.getStatusLine().getStatusCode()!=200){
return Response.status(response.getStatusLine().getStatusCode()).build();
}
//if successful,return the response body
HttpEntity resEntity = response.getEntity();
String responseBody = "";
if (resEntity != null) {
responseBody = EntityUtils.toString(resEntity);
}
EntityUtils.consume(resEntity);
httpClient.getConnectionManager().shutdown();
return Response.status(200).entity(responseBody).build();
}
当我向服务器发送POST请求时,我得到一个错误,告诉我签名(我发送的一个和服务器自己计算的一个)不匹配,所以我想这与签名的基本字符串有关以及POST签名的工作方式,因为它们正在处理双方的密钥和秘密(已选中).
我已经看到,通过这种方式将参数设置为URL的一部分(如在GET请求中).这对我来说不行,因为XML参数可能会超过URL长度,因此需要作为POST参数发送.
我想我在签名POST请求或处理参数时出错了,但我不知道是什么.请你帮帮我吗
P.S:如果我缺乏关于这个问题的上下文,错误跟踪或其他信息,我很抱歉,但我在这里是新手.所以请不要犹豫,要求我更多的信息,如果你需要它.