現在、OAuth-Signpost Java ライブラリを使用して、クライアントから OAuth 認証を実装するサーバーに送信されるリクエストに署名しています。(HttpURLConnection を使用して) GET 要求を行うと、すべて正常に機能します。要求は署名され、パラメーターが含まれ、署名は宛先で一致します。ただし、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<NameValuePair> params = new ArrayList<NameValuePair>();
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 リクエストに署名するか、パラメーターを処理するかのいずれかで何か間違っていると思いますが、それが何であるかはわかりません。助けてください。
PS: この問題に関するコンテキスト、エラー トレース、または追加情報が不足している場合は申し訳ありませんが、私は初心者です。そのため、必要な場合はお気軽にお問い合わせください。