我无法使用Reddit的API登录 [英] I am unable to use Reddit's APIs to log in

查看:385
本文介绍了我无法使用Reddit的API登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Reddit API来做一些事情。我有一切工作,但更改页面和登录。

I'm trying to use the Reddit API to do some stuff. I have everything working but changing pages and logging in.

我需要登录才能使用我的程序,我知道如何使用我得到的cookie,但我只能'设法登录。

I need to login to use my program, I know how to use the cookie I get, but I just can't manage to login.

以下是代码:

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

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

The most typical exception I get is:


java.io.IOException:服务器返回HTTP响应代码:504为URL: http://www.reddit.com/api/login/kagnito/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

java.io.IOException: Server returned HTTP response code: 504 for URL: http://www.reddit.com/api/login/kagnito/ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

但我也收到了很多无效密码消息。

But I have also received a lot of "invalid password" messages.

我怎么解决这个问题?已经好几个小时了!

How might I resolve this? Been at it for hours!

Btw。这是我无法理解的: https://github.com/reddit / reddit / wiki / API%3A-login
我不知道如何发布这个?它应该进入标题,还是?
我不熟悉HTTP协议。
(因此我的项目 - 我正在学习)

Btw. This is what I'm having trouble understanding: https://github.com/reddit/reddit/wiki/API%3A-login I'm not sure how to POST this? Should it go into the header, or ? I'm not that familiar with the HTTP protocol. (Hence my project - I'm learning)

推荐答案

没有深入研究为什么其余部分可能会不工作,有一个问题,你是:

Without delving too much into why the rest of it might not work, there is a problem in that you are:


  • 使用 URLEncoder 对您的帖子数据进行编码:您的帖子数据不会进入网址,因此请勿对其进行编码。

  • Using URLEncoder to encode your post data: your post data is not going to into the URL, so do not encode it.

您没有设置 Content-Length 标题。

这是你应该拥有的开始使用:

Here is what you should have to get started:

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时,你应该明确地安装 Fiddler 是HTTP调试器。您会立即看到问题,因为您的帖子数据看起来与示例完全不同。

When working with APIs like this, you should definetly install Fiddler which is HTTP debugger. You would have immediately seen the problem as your post data would look nothing like the example.

更新:

UPDATE:

这是我刚刚投入测试的一些小代码,它对我进行了身份验证(显然更改 myusername mypassword 给你(不要忘记在URL中更改它):

Here is a little code I just threw into a test and it authenticated me just fine (obviously change myusername and mypassword to yours (don't forget to change it in the URL too):

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

这篇关于我无法使用Reddit的API登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆