diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index 6398039da2..b23ee26f11 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -58,12 +58,7 @@ public class RedditController { Map param = new HashMap(); param.put("api_type", "json"); - param.put("kind", "self"); - param.put("sr", "api"); - // param.put("iden", "XCzyTdJveIcYXNhLJ4a2X9WVDswtx83u"); - // param.put("captcha", "BJMGMU"); - // param.put("title", "http2 is coming soon"); - // param.put("text", "http2 is coming soon what do you think about that"); + param.put("kind", "link"); param.putAll(formParams); System.out.println(param.keySet()); @@ -83,20 +78,33 @@ public class RedditController { @RequestMapping("/post") public String showSubmissionForm(Model model) throws JsonProcessingException, IOException { - String needsCaptchaResult = needsCaptcha(); - if (needsCaptchaResult.equalsIgnoreCase("true")) { - String newCaptchaResult = getNewCaptcha(); - String[] split = newCaptchaResult.split("\""); - String iden = split[split.length - 2]; - model.addAttribute("iden", iden.trim()); + try { + List subreddits = getSubreddit(); + model.addAttribute("subreddits", subreddits); + + String needsCaptchaResult = needsCaptcha(); + if (needsCaptchaResult.equalsIgnoreCase("true")) { + String newCaptchaResult = getNewCaptcha(); + String[] split = newCaptchaResult.split("\""); + String iden = split[split.length - 2]; + model.addAttribute("iden", iden.trim()); + } + } catch (UserApprovalRequiredException e) { + throw e; + } catch (UserRedirectRequiredException e) { + throw e; + } catch (Exception e) { + LOGGER.error("Error occurred", e); + model.addAttribute("error", e.getLocalizedMessage()); + return "reddit"; } return "submissionForm"; } // - public List getSubreddit(Model model) throws JsonProcessingException, IOException { - String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular", String.class); + private List getSubreddit() throws JsonProcessingException, IOException { + String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class); JsonNode node = new ObjectMapper().readTree(result); node = node.get("data").get("children"); List subreddits = new ArrayList(); @@ -106,12 +114,12 @@ public class RedditController { return subreddits; } - public String needsCaptcha() { + private String needsCaptcha() { String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class); return result; } - public String getNewCaptcha() { + private String getNewCaptcha() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity req = new HttpEntity(headers); diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp index d0c53411bc..bde4587fc4 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp @@ -9,7 +9,7 @@

Your Reddit Info

Your reddit username is ${info} -
+


Submit to Reddit
diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp index 87c03adc9f..b2f6ad5eae 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp @@ -17,10 +17,22 @@

- - + +
-

+

+
+ + + + +
+

+