Java 如何使用Signpost和ApacheHTTP客户端发送带有JSON的POST请求
我需要向使用Oauth 1.0进行身份验证的API发送一个带有JSON主体的POST请求。为此,我使用路标和ApacheCommons模块对请求进行签名。我已经确认,带有空白JSON字符串的测试请求可以通过使用Postman并让它为我生成身份验证头来工作 然而,我还没有弄清楚如何使用路标正确设置请求具体来说,我不确定如何将领域添加到请求中。Java 如何使用Signpost和ApacheHTTP客户端发送带有JSON的POST请求,java,oauth,apache-httpclient-4.x,signpost,Java,Oauth,Apache Httpclient 4.x,Signpost,我需要向使用Oauth 1.0进行身份验证的API发送一个带有JSON主体的POST请求。为此,我使用路标和ApacheCommons模块对请求进行签名。我已经确认,带有空白JSON字符串的测试请求可以通过使用Postman并让它为我生成身份验证头来工作 然而,我还没有弄清楚如何使用路标正确设置请求具体来说,我不确定如何将领域添加到请求中。 public void sendRequest(String json) { String baseUrl = properties.getPr
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返回一个错误,即登录请求无效 我没有重复关闭的代表,但我能够在这个问题中找到如何添加领域的答案:我没有重复关闭的代表,但我能够在这个问题中找到如何添加领域的答案: