尝试使用Java向Reddit发布评论

尝试使用Java向Reddit发布评论,java,api,reddit,Java,Api,Reddit,我正试图搞乱一个程序,该程序将登录到reddit帐户,并在线程上发表评论。到目前为止,我的登录代码如下: DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("https://ssl.reddit.com/api/login"); List<NameValuePair> nameValuePairs = new ArrayList<>(4);

我正试图搞乱一个程序,该程序将登录到reddit帐户,并在线程上发表评论。到目前为止,我的登录代码如下:

DefaultHttpClient client = new DefaultHttpClient();

    HttpPost post = new HttpPost("https://ssl.reddit.com/api/login");
    List<NameValuePair> nameValuePairs = new ArrayList<>(4);
    nameValuePairs.add(new BasicNameValuePair("user", "username"));
    nameValuePairs.add(new BasicNameValuePair("passwd", "password"));
    nameValuePairs.add(new BasicNameValuePair("rem", "True"));
    nameValuePairs.add(new BasicNameValuePair("api_type", "json"));

    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(post);

        Header header = response.getFirstHeader("set-cookie");
        String cookie = header.getValue();

        if (cookie.startsWith("reddit_first")) {
            System.out.println("Unable to log in.");
        } else if (cookie.startsWith("reddit_session")) {
            System.out.println("Logged in successfullly.");

            CookieStore cs = new BasicCookieStore();
            System.out.println("Cookie: " + header.getValue());
            BasicClientCookie bcookie = new BasicClientCookie("reddit_session", header.getValue());
            bcookie.setDomain("reddit.com");
            bcookie.setPath("/");
            cs.addCookie(bcookie);
            client.setCookieStore(cs);
            redditCookie = header;
        }

        JSONObject obj = (JSONObject) JSONValue.parse(response.getEntity().getContent());
        JSONObject json = (JSONObject) obj.get("json");
        JSONObject data = (JSONObject) json.get("data");
        modHash = data.get("modhash").toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
DefaultHttpClient=newdefaulthttpclient();
HttpPost=新的HttpPost(“https://ssl.reddit.com/api/login");
List nameValuePairs=新的ArrayList(4);
添加(新的BasicNameValuePair(“用户”、“用户名”);
添加(新的BasicNameValuePair(“passwd”、“password”);
添加(新的BasicNameValuePair(“rem”、“True”);
添加(新的BasicNameValuePair(“api_类型”、“json”);
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=client.execute(post);
Header Header=response.getFirstHeader(“设置cookie”);
字符串cookie=header.getValue();
if(cookie.startsWith(“reddit_first”)){
System.out.println(“无法登录”);
}else if(cookie.startsWith(“reddit_会话”)){
System.out.println(“成功登录”);
CookieStore cs=新的BasicCookieStore();
System.out.println(“Cookie:+header.getValue());
BasicClientCookie bcookie=新的BasicClientCookie(“reddit_会话”,header.getValue());
setDomain(“reddit.com”);
bcookie.setPath(“/”);
cs.addCookie(bcookie);
客户:setCookieStore(cs);
redditCookie=标题;
}
JSONObject obj=(JSONObject)JSONValue.parse(response.getEntity().getContent());
JSONObject json=(JSONObject)obj.get(“json”);
JSONObject数据=(JSONObject)json.get(“数据”);
modHash=data.get(“modHash”).toString();
}捕获(例外e){
e、 printStackTrace();
}
这确实有效——它报告它已成功登录,并且它确实存储了modhash

为了发表评论,我有:

post = new HttpPost("https://ssl.reddit.com/api/comment");
post.addHeader(redditCookie);
nameValuePairs = new ArrayList<>(4);
nameValuePairs.add(new BasicNameValuePair("api_type", "json"));
nameValuePairs.add(new BasicNameValuePair("text", imgurLink + "\r\n"));
nameValuePairs.add(new BasicNameValuePair("thing_id", thingId));
nameValuePairs.add(new BasicNameValuePair("uh", modHash));

try {
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    response = client.execute(post);

    obj = (JSONObject) JSONValue.parse(response.getEntity().getContent());
    System.out.println(obj.toJSONString());
} catch (Exception e) {
    e.printStackTrace();
}
post=新的HttpPost(“https://ssl.reddit.com/api/comment");
post.addHeader(redditCookie);
nameValuePairs=新的ArrayList(4);
添加(新的BasicNameValuePair(“api_类型”、“json”);
添加(新的BasicNameValuePair(“文本”,imgurLink+“\r\n”);
添加(新的BasicNameValuePair(“thing_id”,thingId));
添加(新的BasicNameValuePair(“uh”,modHash));
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
响应=client.execute(post);
obj=(JSONObject)JSONValue.parse(response.getEntity().getContent());
System.out.println(obj.toJSONString());
}捕获(例外e){
e、 printStackTrace();
}
虽然,当我尝试发送评论时,它告诉我需要登录才能发送评论。我知道我必须发送reddit_会话cookie来发布评论,但我不知道我是否做得正确。

你看过吗?它几乎是RedditAPI的完整Java包装