gpt4 book ai didi

java - 用于投票的 Reddit API

转载 作者:行者123 更新时间:2023-11-30 09:42:36 24 4
gpt4 key购买 nike

我可以使用 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);
}

最佳答案

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

关于java - 用于投票的 Reddit API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8569465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com