2016-01-20 40 views
0

請原諒我,如果我問一些愚蠢的東西,我是一個新手在這裏。我需要在我的Java應用程序中實施OAuth以對launchpad.net API進行身份驗證。 documentation指定使用三個參數啓動令牌請求:oauth_consumer_key例如(我的應用程序的名稱),oauth_signature_method例如「PLAINTEXT」和oauth_signature,例如字符串「&」。我意識到大多數OAuth庫要求我已經從OAuth提供者(例如Twitter發佈的)中獲得了消費者密鑰和消費者ID /密鑰,並且大多數示例都以這種方式組織。但是,launchpad.net只有在發出請求令牌(他們不使用第三方提供者)之後纔會發出這些參數。我該如何繼續?我現在在嘗試一些引發錯誤的庫後仍然陷入困境。非常感謝任何有用的信息。正式的launchpad庫在python中。Oauth令牌請求提供者憑據發佈前

我最初的代碼如下:

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

} 
+0

顯示你到目前爲止所嘗試過的,並具體。你有一個合理的問題,但它可能會被關閉,因爲你沒有提供足夠的信息。 –

+0

@MadPhysicist感謝您的有用觀察。我已經添加了我的代碼。我跑步時會得到401。 – SyCode

回答

0

一些研究和代碼重新分解後,我終於解決了這個問題的錯誤消息。正確的代碼在下面,也許它可能對那裏的人有用。

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

}