refresh api token if necessary

master
adb 4 years ago
parent d2f207a0e0
commit 1cb5fe33be

@ -39,7 +39,10 @@ public class RedditAPI {
client = new RedditHttpClient(userAgent, HttpClientBuilder.create().build());
// 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("Token Type: " + token.getTokenType());
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 59 minutes: " + token.willExpireIn((long) 3540));
}
public void newToken() throws RedditOAuthException {
this.token = this.agent.tokenAppOnly(false);
}
public List<Submission> getSub(String subreddit) throws RedditParseException {
public List<Submission> getSub(String subreddit) throws RedditParseException, RedditOAuthException {
if (token.willExpireIn((long) 120)) newToken();
SubmissionsListingParser parser = new SubmissionsListingParser();
SubmissionsOfSubredditRequest request = (SubmissionsOfSubredditRequest) new SubmissionsOfSubredditRequest(subreddit, SubmissionSort.HOT).setLimit(100);
List<Submission> submissions = parser.parse(client.get(token, request));

Loading…
Cancel
Save