3

I need to send a POST request with an JSON body to an API that uses Oauth 1.0 for it's authentication. To do this I am using Signpost and it's Apache Commons module to sign the request. I have confirmed that a test request with a blank JSON string works by using Postman and having it generate the authentication header for me.

However, I have not been able to figure out how to properly set up the request using Signpost. Specifically I'm not sure how to add the realm to the request.

  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();
  }

As is this request returns an error from the API that the login request is invalid.

mentallurg
  • 4,967
  • 5
  • 28
  • 36
MonkeezOnFire
  • 131
  • 1
  • 3
  • I do not have the rep to close as duplicate, but I was able to find the answer on how to add the realm in this question: https://stackoverflow.com/questions/56102784/java-perform-oauth1-0-authenticated-request-with-given-consumerkey-consumerse – MonkeezOnFire Jun 11 '20 at 18:46

0 Answers0