javaspring-bootoauthscribeoauth-1.0a

How to make a POST call using Scribe and fetch data having OAuth 1.0 Authentication in Java?


I have a requirement to make a post-call to a URL which has OAuth 1.0 authentication. I am pretty new to all these. From my research, I got to know about Scribe in Java, but I can find only Get calls using Scribe. I already have consumerKey and consumerSecret key for OAuth 1.0 authentication. Are there any suggestions on how to achieve this successfully.

With postman I am able to fetch the data successfully, but I want to achieve it using Java.

I have tried something like this

I tried this way

public String getSmartCommPDF(@RequestBody Model model) throws IOException {
        OAuthService service = new ServiceBuilder().provider(ModelAPI.class).apiKey(consumerKey)
                .apiSecret(consumerSecret).build();

        OAuthRequest request = new OAuthRequest(Verb.POST, url);
        ObjectMapper mapper = new ObjectMapper();
        request.addHeader("Content-Type", "application/json;charset=UTF-8");
        request.addPayload(mapper.writeValueAsString(model));

        Token accessToken = new Token("", ""); // not required for context.io
        service.signRequest(accessToken, request);
        Response response = request.send();
        System.out.println("Response = " + response.getBody());
        return "Success";
    }

This is my ModelAPI class

public class ModelAPI extends DefaultApi10a {

    @Override
    public String getRequestTokenEndpoint() {
        return "https://domain/one/oauth1/api/v6/job";
    }

    @Override
    public String getAccessTokenEndpoint() {
        return "https://domain/one/oauth1/api/v6/job";
    }

    @Override
    public String getAuthorizationUrl(Token requestToken) {
        return "https://domain/one/oauth1/api/v6/job";
    }

}

This part of code is not throwing any error but, the response body is empty. Where I am going wrong, any one has any idea?

Thank you.


Solution

  • The data was coming back in the input stream. So, I used

    response.getStream();
    

    and write it to a file and use it.