用于投票的Reddit API

时间:2011-12-20 00:39:58

标签: java api reddit

我可以通过API使用Java登录reddit,但我无法进行投票处理。这是为了让它发挥作用而尝试减少案例。但我得到的只是"用户必须这样做&#34 ;;任何想法?

String apiParams = "api_type=json&id=c38ghjg&dir=1&uh=" + modHash;

URL voteURL = new URL("http://www.reddit.com/api/vote");
HttpURLConnection connection = (HttpURLConnection) voteURL.openConnection ();
connection.setDoOutput (true);
connection.setRequestMethod ("POST");
connection.setUseCaches (false);
connection.setRequestProperty ("Content-Type",
                            "application/x-www-form-urlencoded; charset=UTF-8" );
connection.setRequestProperty("cookie", "reddit_session="+cookie);
connection.setRequestProperty ("Content-Length",
                            String.valueOf( apiParams.length() ));
DataOutputStream wr = new DataOutputStream( connection.getOutputStream() );
wr.writeBytes( apiParams );
wr.flush();
wr.close();
InputStream cis = connection.getInputStream();

HashMap<String, String> parameters = new HashMap<String,String> ();

if(cis != null){
    ObjectMapper mapper = new ObjectMapper ();

    TypeReference<Map<String, Object>>
    mapReference = new TypeReference<Map<String, Object>> () { };

    Map<String, Object> resultJSON = mapper.readValue (cis, mapReference);

    Map<String, Object> json = (Map<String, Object>) resultJSON.get ("json");
    Map<String, Object> data = (Map<String,Object>) json.get ("data");
    System.out.println(json);
}

1 个答案:

答案 0 :(得分:2)

您是否先设置了reddit_session cookie?在访问投票等功能之前,您必须这样做。 (您必须通过登录json获取cookie。)