如何使用jraw生成Reddit保存项目列表?

时间:2016-08-11 02:11:32

标签: java api reddit

我正在尝试使用JRAW生成所有已保存的reddit项目的列表。

我已经浏览了Quickstart,并成功设法登录并检索信息,我可以从Cookbook获取首页上的项目列表,但我无法解决我如何得到我保存的项目列表(评论和帖子)或我自己的帖子列表(也是评论和帖子)。

保存的项目位于https://www.reddit.com/user/<username>/saved/,但我不知道如何让jraw检索和解析,或者api使用不同的URL。

编辑:我想我可能需要使用UserContributionPaginator,但我还没有完全确定如何让它工作。

1 个答案:

答案 0 :(得分:1)

完成了工作。

package com.jraw;

import net.dean.jraw.RedditClient;
import net.dean.jraw.http.UserAgent;
import net.dean.jraw.http.oauth.Credentials;
import net.dean.jraw.http.oauth.OAuthData;
import net.dean.jraw.http.oauth.OAuthException;
import net.dean.jraw.models.Contribution;
import net.dean.jraw.models.Listing;
import net.dean.jraw.paginators.UserContributionPaginator;

public class printSaved {

    public static void main(String [] args) {
        UserAgent myUserAgent = UserAgent.of("desktop", "com.jraw.printSaved", "v0.01", "user");
        RedditClient redditClient = new RedditClient(myUserAgent);
        String username = "username";
        Credentials credentials = Credentials.script(username, "<password>", "<clientId>", "<clientSecret>");

        OAuthData authData = null;
        try {
            authData = redditClient.getOAuthHelper().easyAuth(credentials);
        } catch (OAuthException e) {
            e.printStackTrace();
        }
        redditClient.authenticate(authData);

        UserContributionPaginator saved = new UserContributionPaginator(redditClient,"saved",username);

        Listing<Contribution> savedList = saved.next();

        for (Contribution item : savedList) {
            System.out.println(item);
        }
    }
}