我需要将带有 JSON 正文的 POST 请求发送到使用 Oauth 1.0 进行身份验证的 API。为此,我使用 Signpost 和它的 Apache Commons 模块来签署请求。我已经确认带有空白 JSON 字符串的测试请求可以通过使用 Postman 并让它为我生成身份验证标头来工作。
但是,我无法弄清楚如何使用 Signpost 正确设置请求。具体来说,我不确定如何将领域添加到请求中。
public void sendRequest(String json) {
String baseUrl = properties.getProperty("apiUrl");
String consumerKey = properties.getProperty("consumerKey");
String consumerSecret = properties.getProperty("consumerSecret");
String tokenKey = properties.getProperty("tokenKey");
String tokenSecret = properties.getProperty("tokenSecret");
String realm = properties.getProperty("realm");
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
consumer.setTokenWithSecret(tokenKey, tokenSecret);
HttpPost request = new HttpPost(baseUrl + "/site/restlet.nl?script=2162&deploy=1");
System.out.println("requesting " + request.toString());
consumer.sign(request);
request.setEntity(new StringEntity(json));
request.setHeader("Content-type", "application/json");
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(request);
System.out.println("got response " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase());
client.close();
}
同样,此请求从 API 返回登录请求无效的错误。