Java 颁发提供程序凭据之前的Oauth令牌请求
如果我问了一些愚蠢的问题,请原谅,我是这里的新手。我需要在Java应用程序中实现OAuth,以根据launchpad.net API进行身份验证。使用三个参数指定令牌请求的启动:oauth_consumer_key,例如(我的应用程序的名称),oauth_signature_方法,例如“明文”和oauth_signature,例如字符串“&”。我意识到大多数OAuth库都需要这样做 我已经从中获取了消费者密钥和消费者Id/Secret OAuth提供者(例如在Twitter上发布的),大多数示例都是以这种方式组织的。但是,launchpad.net仅在发出请求令牌后才会发出这些参数(它们不使用第三方提供程序)。如何继续?在尝试了一些抛出错误的库之后,我目前陷入了困境。非常感谢您提供的有用信息。官方的launchpad库是用python编写的 我的初始代码如下:Java 颁发提供程序凭据之前的Oauth令牌请求,java,rest,oauth,launchpad,signpost,Java,Rest,Oauth,Launchpad,Signpost,如果我问了一些愚蠢的问题,请原谅,我是这里的新手。我需要在Java应用程序中实现OAuth,以根据launchpad.net API进行身份验证。使用三个参数指定令牌请求的启动:oauth_consumer_key,例如(我的应用程序的名称),oauth_signature_方法,例如“明文”和oauth_signature,例如字符串“&”。我意识到大多数OAuth库都需要这样做 我已经从中获取了消费者密钥和消费者Id/Secret OAuth提供者(例如在Twitter上发布的),大多数示例
public class Quicky {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet("https://launchpad.net/+request-token");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
System.out.println("Your current GET request status:" + response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
EntityUtils.consume(entity1);
} finally {
response1.close();
}
HttpRequest request;
HttpPost httpPost = new HttpPost("https://launchpad.net/+request-token");
PostMethod poster = new PostMethod();
List <NameValuePair> postParams = new ArrayList <NameValuePair>();
postParams.add(new BasicNameValuePair("oauth_customer_key", "XXXX"));
postParams.add(new BasicNameValuePair("oauth_signature_method", "PLAINTEXT"));
postParams.add(new BasicNameValuePair("oauth_signature", "&"));
httpPost.setEntity(new UrlEncodedFormEntity(postParams, "utf-8"));
// httpPost.setEntity(entity1);
httpclient.execute(httpPost);
HttpParameters requestParams = (HttpParameters) postParams;
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println("Your current POST request status:" + response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
response2.close();
}
} finally {
httpclient.close();
}
}
}
快速公开类{
公共静态void main(字符串[]args)引发异常{
CloseableHttpClient httpclient=HttpClients.createDefault();
试一试{
HttpGet HttpGet=新的HttpGet(“https://launchpad.net/+请求令牌);
CloseableHttpResponse response1=httpclient.execute(httpGet);
试一试{
System.out.println(“您当前的GET请求状态:+response1.getStatusLine());
HttpEntity entity1=response1.getEntity();
EntityUtils.consume(entity1);
}最后{
响应1.close();
}
HttpRequest请求;
HttpPost HttpPost=新的HttpPost(“https://launchpad.net/+请求令牌);
PostMethod海报=新的PostMethod();
List postParams=newarraylist();
添加(新的BasicNameValuePair(“oauth_客户密钥”,“XXXX”);
添加(新的BasicNameValuePair(“oauth_签名_方法”,“明文”);
postParams.add(新的BasicNameValuePair(“oauth_签名”);
setEntity(新的UrlEncodedFormEntity(后参数,“utf-8”);
//httpPost.setEntity(entity1);
httpclient.execute(httpPost);
HttpParameters requestParams=(HttpParameters)后参数;
CloseableHttpResponse response2=httpclient.execute(httpPost);
试一试{
System.out.println(“您当前的POST请求状态:+response2.getStatusLine());
HttpEntity entity2=response2.getEntity();
//对响应体执行一些有用的操作
//并确保它被完全消耗
EntityUtils.consume(entity2);
}最后{
response2.close();
}
}最后{
httpclient.close();
}
}
}
经过一些研究和代码重新分解,我终于解决了错误消息的问题。下面是正确的代码,可能对其他人有用
public class LaunchPadTokenRetriever {
public static void main(String[] args) throws ClientProtocolException, IOException{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://launchpad.net/+request-token");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
List <NameValuePair> urlParams = new ArrayList <NameValuePair>();
urlParams.add(new BasicNameValuePair("oauth_signature", "&"));
urlParams.add(new BasicNameValuePair("oauth_consumer_key", "tester"));
urlParams.add(new BasicNameValuePair("oauth_signature_method", "PLAINTEXT"));
httpPost.setEntity(new UrlEncodedFormEntity(urlParams));
CloseableHttpResponse response = httpclient.execute(httpPost);
System.out.println(response);
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpPost, responseHandler);
System.out.println("Initial credentials ---> "+ responseBody);
System.out.println();
String getresponse = responseBody;
EntityUtils.consume(entity);
} finally {
response.close();
}
}
}
公共类LaunchPadTokenRetriever{
公共静态void main(字符串[]args)抛出ClientProtocolException,IOException{
CloseableHttpClient httpclient=HttpClients.createDefault();
HttpPost HttpPost=新的HttpPost(“https://launchpad.net/+请求令牌);
addHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
List urlParams=newarraylist();
添加(新的BasicNameValuePair(“oauth_签名”、“&”);
添加(新的BasicNameValuePair(“oauth_consumer_key”、“tester”);
添加(新的BasicNameValuePair(“oauth_签名_方法”,“明文”);
setEntity(新的UrlEncodedFormEntity(urlParams));
CloseableHttpResponse response=httpclient.execute(httpPost);
System.out.println(响应);
试一试{
System.out.println(response.getStatusLine());
HttpEntity=response.getEntity();
ResponseHandler ResponseHandler=新BasicResponseHandler();
字符串responseBody=httpclient.execute(httpPost,responseHandler);
System.out.println(“初始凭证-->”+响应库);
System.out.println();
字符串getresponse=responseBody;
EntityUtils.consume(实体);
}最后{
response.close();
}
}
}
展示您迄今为止所做的尝试,并具体说明。你有一个合理的好问题,但它可能会被关闭,因为你没有提供足够的信息。@MadPhysicator谢谢你有用的观察。我已经添加了我的代码。我跑步的时候会拿到401。