java - 尝试使用 Java 向 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);
    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();
    }

这确实有效 - 它报告它已成功登录,并且它确实存储了一个 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();
}

虽然,当我尝试发送评论时,它告诉我需要登录才能执行此操作。我知道我必须发送 reddit_session cookie 才能发表评论,但我不知道我这样做是否正确。

最佳答案

你看过jReddit了吗? ?它是 Reddit API 的几乎完整的 Java 包装器。

关于java - 尝试使用 Java 向 Reddit 发表评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17352007/

相关文章:

java - Spring向数据库添加数据,BindingResult结果有Errors

api - 在正常路线旁边使用 Swagger for Express

python - 使用 praw 从 reddit 获取前 10 名帖子

python - PRAW:Python Reddit API 包装器不工作

java - 甲骨文对谷歌的诉讼对 Android 开发者意味着什么?

java - 在自定义 ArrayList 中仅添加一次项目

c# - ASP.net mvc API 获取请求和 JSON 数据

javascript - 如何从reddit获取随机帖子

java - java中如何处理抛出检查异常的方法

java - 如何处理 REST API 中的响应重定向?