代码之家  ›  专栏  ›  技术社区  ›  SyCode

颁发提供程序凭据之前的Oauth令牌请求

  •  0
  • SyCode  · 技术社区  · 9 年前

    如果我问了一些愚蠢的问题,请原谅我,我是这里的新手。我需要在Java应用程序中实现OAuth,以便根据启动板进行身份验证。net API。这个 documentation 使用三个参数指定令牌请求的启动:oauth_consumer_key例如(我的应用程序的名称)、oauth_signature_method例如“PLAINTEXT”和oauth_sgnature例如字符串“&”。我意识到大多数OAuth库都需要 我已经从中获取了消费者密钥和消费者Id/密码 OAuth提供者(例如在Twitter上发布的),大多数示例都是以这种方式组织的。然而,发射台。net仅在发出请求令牌后才发出这些参数(它们不使用第三方提供者)。我如何继续?我目前在尝试了一些抛出错误的库之后陷入了困境。非常感谢您提供的有用信息。官方的launchpad库在python中。

    我的初始代码如下:

    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();
            }
        }
    
    }
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   SyCode    9 年前

    经过一些研究和代码重构,我终于解决了问题错误消息。正确的代码在下面,也许它对外面的人有用。

        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();
        }
      }
    
    }