2011-11-14 24 views
5

私はReddit APIを使って何かをしようとしています。私はRedditのAPIを使ってログインすることができません

私のプログラムを使用するにはログインする必要がありますが、私は取得したクッキーの使い方を知っていますが、ログインできません。ここで

はコードです:

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://www.reddit.com/api/login/kagnito/ :URLの504:サーバーは、HTTP応答コードを返しました。 net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

しかし、私はまた多くの「無効なpa ssword "メッセージを生成します。

どうすれば解決できますか?それは何時間もありました!

Btw。これは私が理解できないことです:https://github.com/reddit/reddit/wiki/API%3A-login 私はこれをPOSTする方法がわかりません?ヘッダに入るべきか、それとも? 私はHTTPプロトコルに精通していません。 - あまりにも多くのそれの残りの部分が動作しない理由を掘り下げるなし

+0

私の答えへの更新をチェックし、あなたが良識を作った、と私は今、それをコードするように変更されていると述べた – Strelok

答えて

9

(それゆえ私のプロジェクト私が学んでいる)、という問題があるあなたは、次のとおりです。エンコードするためにURLEncoderを使用し

  • あなた投稿データ:投稿データはURLには入りませんので、エンコードしないでください。

  • 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 HTTPデバッガですFiddlerをインストールする必要があります。あなたのポストデータは例のようには見えないので、すぐに問題を見たでしょう。

UPDATE:

ここで私はちょうどテストに投げ、それがURLでそれを変更することを忘れないでください(明らかにあなたにmyusernamemypasswordを変更する(私はうまく認証された少しのコードがありますあまりにも):

@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

ありがとう!それはうまくいった! 私はあなたのコードを読んで、私が上に移動する前にそれを理解します。 しかし、本当に、ありがとう! –

関連する問題