Fix reddit submit
This commit is contained in:
parent
ea84d8cc3e
commit
04bd9e72ed
|
@ -17,6 +17,8 @@ import org.springframework.security.oauth2.client.resource.UserApprovalRequiredE
|
||||||
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
|
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
@ -43,7 +45,7 @@ public class RedditController {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.error("Error occurred", e);
|
LOGGER.error("Error occurred", e);
|
||||||
model.addAttribute("error", e.getLocalizedMessage());
|
model.addAttribute("error", e.getMessage());
|
||||||
}
|
}
|
||||||
return "reddit";
|
return "reddit";
|
||||||
}
|
}
|
||||||
|
@ -51,20 +53,21 @@ public class RedditController {
|
||||||
@RequestMapping("/submit")
|
@RequestMapping("/submit")
|
||||||
public String submit(Model model, @RequestParam Map<String, String> formParams) {
|
public String submit(Model model, @RequestParam Map<String, String> formParams) {
|
||||||
try {
|
try {
|
||||||
System.out.println(formParams.keySet());
|
MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
|
||||||
HttpHeaders headers = new HttpHeaders();
|
param.add("api_type", "json");
|
||||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
param.add("kind", "link");
|
||||||
HttpEntity req = new HttpEntity(headers);
|
param.add("resubmit", "true");
|
||||||
|
param.add("sendreplies", "false");
|
||||||
|
param.add("then", "comments");
|
||||||
|
|
||||||
Map<String, String> param = new HashMap<String, String>();
|
for (Map.Entry<String, String> entry : formParams.entrySet()) {
|
||||||
param.put("api_type", "json");
|
param.add(entry.getKey(), entry.getValue());
|
||||||
param.put("kind", "link");
|
}
|
||||||
param.putAll(formParams);
|
|
||||||
|
|
||||||
System.out.println(param.keySet());
|
LOGGER.info("User submitting Link with these parameters: " + formParams.entrySet());
|
||||||
System.out.println(param.entrySet());
|
String result = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, String.class);
|
||||||
ResponseEntity<String> result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/submit", req, String.class, param);
|
LOGGER.info("Full Reddit Response: " + result);
|
||||||
String responseMsg = parseResponseMessage(result.getBody());
|
String responseMsg = parseResponse(result);
|
||||||
model.addAttribute("msg", responseMsg);
|
model.addAttribute("msg", responseMsg);
|
||||||
} catch (UserApprovalRequiredException e) {
|
} catch (UserApprovalRequiredException e) {
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -97,7 +100,7 @@ public class RedditController {
|
||||||
return "submissionForm";
|
return "submissionForm";
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
// === private
|
||||||
|
|
||||||
private List<String> getSubreddit() throws JsonProcessingException, IOException {
|
private List<String> getSubreddit() throws JsonProcessingException, IOException {
|
||||||
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class);
|
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class);
|
||||||
|
@ -128,15 +131,20 @@ public class RedditController {
|
||||||
return split[split.length - 2];
|
return split[split.length - 2];
|
||||||
}
|
}
|
||||||
|
|
||||||
private String parseResponseMessage(String responseBody) {
|
private String parseResponse(String responseBody) throws JsonProcessingException, IOException {
|
||||||
System.out.println(responseBody);
|
String result = "";
|
||||||
int index = responseBody.indexOf("error");
|
JsonNode node = new ObjectMapper().readTree(responseBody);
|
||||||
if (index == -1) {
|
JsonNode errorNode = node.get("json").get("errors").get(0);
|
||||||
return "Post submitted successfully";
|
for (JsonNode child : errorNode) {
|
||||||
} else {
|
result = result + child.toString().replaceAll("\"|null", "") + "<br>";
|
||||||
int msgEnd = responseBody.indexOf("\"", index);
|
|
||||||
return responseBody.substring(index, msgEnd);
|
|
||||||
}
|
}
|
||||||
|
if (result.length() == 0) {
|
||||||
|
if (node.get("json").get("data") != null && node.get("json").get("data").get("url") != null)
|
||||||
|
return "Post submitted successfully <a href=\"" + node.get("json").get("data").get("url").asText() + "\"> check it out </a>";
|
||||||
|
else
|
||||||
|
return "Error Occurred";
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) {
|
public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) {
|
||||||
|
|
|
@ -3,14 +3,17 @@
|
||||||
<head>
|
<head>
|
||||||
|
|
||||||
<title>Spring Security OAuth</title>
|
<title>Spring Security OAuth</title>
|
||||||
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<div class="container">
|
||||||
<c:choose>
|
<c:choose>
|
||||||
<c:when test="${info != null}">
|
<c:when test="${info != null}">
|
||||||
<h1>Your Reddit Info</h1>
|
<h1>Your Reddit Info</h1>
|
||||||
<b>Your reddit username is </b>${info}
|
<b>Your reddit username is </b>${info}
|
||||||
<br><br><br>
|
<br><br><br>
|
||||||
<a href="post">Submit to Reddit</a>
|
<a href="post" class="btn btn-primary">Submit to Reddit</a>
|
||||||
</c:when>
|
</c:when>
|
||||||
<c:otherwise>
|
<c:otherwise>
|
||||||
<b>Sorry, error occurred</b>
|
<b>Sorry, error occurred</b>
|
||||||
|
@ -18,5 +21,6 @@
|
||||||
<div>${error}</div>
|
<div>${error}</div>
|
||||||
</c:otherwise>
|
</c:otherwise>
|
||||||
</c:choose>
|
</c:choose>
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -3,10 +3,13 @@
|
||||||
<head>
|
<head>
|
||||||
|
|
||||||
<title>Spring Security OAuth</title>
|
<title>Spring Security OAuth</title>
|
||||||
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<div class="container">
|
||||||
<h1>${msg}</h1>
|
<h1>${msg}</h1>
|
||||||
<a href="post">Submit another link to Reddit</a>
|
<a href="post" class="btn btn-primary">Submit another link to Reddit</a>
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -5,11 +5,12 @@
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||||
<title>Spring Security OAuth</title>
|
<title>Spring Security OAuth</title>
|
||||||
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<div class="container">
|
||||||
<h1>Welcome to Spring Security OAuth</h1>
|
<h1>Welcome to Spring Security OAuth</h1>
|
||||||
<a href="info">Login with Reddit</a>
|
<a href="info" class="btn btn-primary">Login with Reddit</a>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue