refresh api token if necessary

master
adb 3 years ago
parent d2f207a0e0
commit 1cb5fe33be

@ -39,7 +39,10 @@ public class RedditAPI {
client = new RedditHttpClient(userAgent, HttpClientBuilder.create().build()); client = new RedditHttpClient(userAgent, HttpClientBuilder.create().build());
// Create token (will be valid for 1 hour) // Create token (will be valid for 1 hour)
token = agent.tokenAppOnly(false); newToken();
}
public void newToken() throws RedditOAuthException {
this.token = this.agent.tokenAppOnly(false);
System.out.println("Access Token: " + token.getAccessToken()); System.out.println("Access Token: " + token.getAccessToken());
System.out.println("Token Type: " + token.getTokenType()); System.out.println("Token Type: " + token.getTokenType());
System.out.println("Refreshable: " + token.isRefreshable()); System.out.println("Refreshable: " + token.isRefreshable());
@ -48,10 +51,8 @@ public class RedditAPI {
System.out.println("Will expire in 61 minutes: " + token.willExpireIn((long) 3660)); System.out.println("Will expire in 61 minutes: " + token.willExpireIn((long) 3660));
System.out.println("Will expire in 59 minutes: " + token.willExpireIn((long) 3540)); System.out.println("Will expire in 59 minutes: " + token.willExpireIn((long) 3540));
} }
public void newToken() throws RedditOAuthException { public List<Submission> getSub(String subreddit) throws RedditParseException, RedditOAuthException {
this.token = this.agent.tokenAppOnly(false); if (token.willExpireIn((long) 120)) newToken();
}
public List<Submission> getSub(String subreddit) throws RedditParseException {
SubmissionsListingParser parser = new SubmissionsListingParser(); SubmissionsListingParser parser = new SubmissionsListingParser();
SubmissionsOfSubredditRequest request = (SubmissionsOfSubredditRequest) new SubmissionsOfSubredditRequest(subreddit, SubmissionSort.HOT).setLimit(100); SubmissionsOfSubredditRequest request = (SubmissionsOfSubredditRequest) new SubmissionsOfSubredditRequest(subreddit, SubmissionSort.HOT).setLimit(100);
List<Submission> submissions = parser.parse(client.get(token, request)); List<Submission> submissions = parser.parse(client.get(token, request));

Loading…
Cancel
Save