简体   繁体   English

如何使用 Scribe 进行 POST 调用并在 Java 中获取具有 OAuth 1.0 身份验证的数据?

[英]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.我需要对具有 OAuth 1.0 身份验证的 URL 进行后调用。 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.通过我的研究,我在 Java 中了解了 Scribe,但我只能找到使用 Scribe 的 Get 调用。 I already have consumerKey and consumerSecret key for OAuth 1.0 authentication.我已经有用于 OAuth 1.0 身份验证的 consumerKey 和 consumerSecret 密钥。 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.使用 postman 我能够成功获取数据,但我想使用 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这是我的 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.谢谢你。

The data was coming back in the input stream.数据在输入 stream 中返回。 So, I used所以,我用

response.getStream();

and write it to a file and use it.并将其写入文件并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM