2

我可以使用 Java 通过 API 登录 reddit,但我无法获得投票处理。这是为了让它工作而尝试做一个简化的案例。但我得到的只是“用户必须这样做”;有任何想法吗?

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

1 回答 1

2

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

于 2012-02-04T20:38:09.243 回答