【发布时间】:2019-05-15 00:54:42
【问题描述】:
我正在尝试向给定的受 oAuth1.0 保护的端点(提供给我的端点的所有者)发送 http 帖子:
- consumerKey
- 消费者秘密
- 访问令牌
- accessTokenSecret
- 领域
我根据How to call API (Oauth 1.0)?写了一些代码
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 : header is not NLAuth scheme。
我注意到域值实际上从未在 oAuthConsumer 配置中设置,我试图找到一种方法来指定域,但我还没有找到方法。
有人知道吗?
【问题讨论】:
标签: java oauth httprequest