Java - 使用给定的 consumerKey、consumerSecret、accessToken、accessTokenSecret 和领域执行 oAuth1.0 认证请求 [英] Java - Perform oAuth1.0 authenticated request with given consumerKey, consumerSecret, accessToken, accessTokenSecret and realm
问题描述
我正在尝试向给定的受 oAuth1.0 保护的端点发送一个 http 帖子,该端点的所有者提供给我:
I am trying to send an http post to a given oAuth1.0 protected endpoint, the owner of the endpoint provided to me:
- 消费者密钥
- 消费者秘密
- 访问令牌
- accessTokenSecret
- 领域
public class HttpAuthPost {
public HttpAuthPost() {
realmID = "XXXXXXX";
String consumerKey = "kjahsdkjhaskdjhaskjdhkajshdkajsd";
String consumerSecret = "jklahsdkjhaskjdhakjsd";
String accessToken = "iuyhiuqhwednqkljnd";
String accessTokenSecret = "oihkhnasdiguqwd56qwd";
setupContext(consumerKey, consumerSecret, accessToken, accessTokenSecret);
}
public void setupContext(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) {
this.oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
oAuthConsumer.setTokenWithSecret(accessToken, accessTokenSecret);
oAuthConsumer.setSigningStrategy(new AuthorizationHeaderSigningStrategy());
}
public void authorize(HttpRequestBase httpRequest) throws FMSException {
try {
oAuthConsumer.sign(httpRequest);
} catch (OAuthMessageSignerException e) {
throw new FMSException(e);
} catch (OAuthExpectationFailedException e) {
throw new FMSException(e);
} catch (OAuthCommunicationException e) {
throw new FMSException(e);
}
}
public String executeGetRequest(String customURIString, String _content) throws UnsupportedEncodingException {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpRequest = null;
//Preparing HttpEntity and populating httpRequest
try {
authorize(httpRequest);
} catch (FMSException e) {
e.printStackTrace();
}
HttpResponse httpResponse = null;
try {
HttpHost target = new HttpHost(uri.getHost(), -1, uri.getScheme());
httpResponse = client.execute(target, httpRequest);
// Process response and generate output
return output;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我进行了一些测试,但收到此错误:USER_ERROR : 标头不是 NLAuth 方案.
I did some tests and I am getting this error: USER_ERROR : header is not NLAuth scheme.
我注意到在 oAuthConsumer 配置中从未真正设置过领域值,我尝试找到一种方法来指定领域,但我还没有找到方法.
I noticed the realm value is never actually set in the oAuthConsumer configuration, I try to find a way to specify the realm but I have not found a way to do it.
有人知道这个吗?
推荐答案
嗯,解决方案实际上非常简单,现在我想通了,这似乎很明显.将领域作为附加参数添加到 authconsumer 对我有用.
Well the solution was actually pretty simple and now that I figured it out it seems obvious. Adding the realm as an additional parameter to the authconsumer worked for me.
希望这对未来的其他人有所帮助.
Hope this help someone else in the future.
public void setupContext(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) {
this.oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
oAuthConsumer.setTokenWithSecret(accessToken, accessTokenSecret);
oAuthConsumer.setSigningStrategy(new AuthorizationHeaderSigningStrategy());
HttpParameters parameters = new HttpParameters();
parameters.put("realm", realmID);
oAuthConsumer.setAdditionalParameters(parameters);
}
这篇关于Java - 使用给定的 consumerKey、consumerSecret、accessToken、accessTokenSecret 和领域执行 oAuth1.0 认证请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!