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

huangapple go评论40阅读模式
英文:

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

问题

我需要进行一个 post 请求到一个需要 OAuth 1.0 认证的 URL。我对这方面还比较陌生。从我的研究中,我了解到 Java 中的 Scribe,但我只找到了使用 Scribe 进行 Get 请求的示例。我已经有了 OAuth 1.0 认证的 consumerKey 和 consumerSecret。

用 Postman 我能够成功获取数据,但我想使用 Java 来实现它。

我尝试了以下方式:

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";
}

这是我的 ModelAPI 类:

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";
    }

}

这部分代码没有抛出任何错误,但响应体为空。我在哪里出错了,有没有人有任何想法?

谢谢。

英文:

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.

答案1

得分: 0

数据是通过输入流返回的。所以,我使用了

response.getStream();

并将其写入文件并使用它。

英文:

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

response.getStream();

and write it to a file and use it.

huangapple
  • 本文由 发表于 2020年7月30日 18:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63170795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定