2011-11-14 116 views
5

我正在嘗試使用Reddit API來做一些事情。我有一切工作,但更改頁面和登錄。我無法使用Reddit的API登錄

我需要登錄才能使用我的程序,我知道如何使用我得到的cookie,但我無法管理登錄。

下面的代碼:

public static Login POST(URL url, String user, String pw) throws IOException 
{ 

    String encodedData = URLEncoder.encode("api_type=json&user=" + user +"&passwd="+pw, "UTF-8"); 
    HttpURLConnection ycConnection = null; 
    ycConnection = (HttpURLConnection) url.openConnection(); 
    ycConnection.setRequestMethod("POST"); 
    ycConnection.setDoOutput(true); 
    ycConnection.setUseCaches (false); 
    ycConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
    PrintWriter out = new PrintWriter(ycConnection.getOutputStream()); 


    out.print(encodedData.getBytes()); 
    out.close(); 

    BufferedReader in = new BufferedReader(new InputStreamReader(ycConnection.getInputStream())); 
    String response = in.readLine(); 

    Map<String, List<String>> headers = ycConnection.getHeaderFields(); 
    List<String> values = headers.get("Set-Cookie"); 
    String cookieValue = null; 
    for (java.util.Iterator<String> iter = values.iterator(); iter.hasNext();) { 
     String v = iter.next(); 
     if (cookieValue == null) 
      cookieValue = v; 
     else 
      cookieValue = cookieValue + ";" + v; 
    } 

    return new Login(cookieValue, response); 
} 

最典型的例外,我得到的是:

產生java.io.IOException:服務器返回的HTTP響應代碼:504網址:http://www.reddit.com/api/login/kagnito/ 在陽光下。 net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

但是我也收到了很多「invalid pa ssword「消息。

我該如何解決這個問題?一直在它幾個小時!

Btw。這是我無法理解的:https://github.com/reddit/reddit/wiki/API%3A-login 我不知道如何發佈這個?它應該進入標題,還是? 我不熟悉HTTP協議。 (因此我的項目 - 我在學習)

+0

檢查更新到我的回答 – Strelok

回答

9

在沒有研究太多了爲什麼它的其餘部分可能無法正常工作,有這樣的問題:你是:

  • 使用URLEncoder編碼您發佈數據:您的發佈數據不會進入網址,因此請勿對其進行編碼。

  • 您未設置Content-Length標題。

這裏是你應該有什麼上手:

public static Login POST(URL url, String user, String pw) throws IOException 
{ 

    String data= "api_type=json&user=" + user +"&passwd="+pw; 
    HttpURLConnection ycConnection = null; 
    ycConnection = (HttpURLConnection) url.openConnection(); 
    ycConnection.setRequestMethod("POST"); 
    ycConnection.setDoOutput(true); 
    ycConnection.setUseCaches (false); 
    ycConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
    ycConnection.setRequestProperty("Content-Length", data.length()); 

    PrintWriter out = new PrintWriter(ycConnection.getOutputStream()); 


    out.print(data.getBytes()); 
    out.close(); 

    BufferedReader in = new BufferedReader(new InputStreamReader(ycConnection.getInputStream())); 
    String response = in.readLine(); 

    Map<String, List<String>> headers = ycConnection.getHeaderFields(); 
    List<String> values = headers.get("Set-Cookie"); 
    String cookieValue = null; 
    for (java.util.Iterator<String> iter = values.iterator(); iter.hasNext();) { 
     String v = iter.next(); 
     if (cookieValue == null) 
      cookieValue = v; 
     else 
      cookieValue = cookieValue + ";" + v; 
    } 

    return new Login(cookieValue, response); 
} 

當這樣的API的工作,你應該安裝definetly這Fiddler是HTTP調試器。你會立即看到這個問題,因爲你的發佈數據看起來不像這個例子。

UPDATE:

這裏是一個小的代碼,我只是把到測試和驗證它我就好了(明顯變化myusernamemypassword到你的(不要忘記去改變它的URL太):

@Test 
    public void someTest() throws IOException 
    { 
     URL u = new URL("https://ssl.reddit.com/api/login/myusername"); 
     login(u, "myusername", "mypassword"); 
    } 

    public static void login(URL url, String user, String pw) throws IOException 
    { 

     String data = "api_type=json&user=" + user + "&passwd=" + pw; 
     HttpURLConnection ycConnection = null; 
     ycConnection = (HttpURLConnection) url.openConnection(); 
     ycConnection.setRequestMethod("POST"); 
     ycConnection.setDoOutput(true); 
     ycConnection.setUseCaches(false); 
     ycConnection.setRequestProperty("Content-Type", 
      "application/x-www-form-urlencoded; charset=UTF-8"); 
     ycConnection.setRequestProperty("Content-Length", String.valueOf(data.length())); 

     DataOutputStream wr = new DataOutputStream(
      ycConnection.getOutputStream()); 
     wr.writeBytes(data); 
     wr.flush(); 
     wr.close(); 
     InputStream is = ycConnection.getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     String line; 
     StringBuffer response = new StringBuffer(); 
     while ((line = rd.readLine()) != null) 
     { 
      response.append(line); 
      response.append('\r'); 
     } 
     for (Entry< String, List<String>> r : ycConnection.getHeaderFields().entrySet()) 
     { 
      System.out.println(r.getKey() + ": " + r.getValue()); 
     } 
     rd.close(); 
     System.out.println(response.toString()); 
    } 
+0

一切你說取得了良好的感覺,現在我已經改變了代碼到out.close(後到來 一切),除了發球r的迴應是無關緊要的,那還不是問題,所以謝謝你不要潛入那裏。 但問題仍然存在,我無法獲得認證。 –

+0

請忽略拼寫錯誤,今晚沒有多少睡眠。 –

+0

非常感謝!這工作! 在繼續之前,我會通讀並理解它。 但真的,謝謝! –