From db654ddfdd2b5438b2a6ddb56c611bd193f24013 Mon Sep 17 00:00:00 2001 From: adb Date: Fri, 22 Jan 2021 02:27:39 +0100 Subject: [PATCH] move handler + start reddit session --- build.gradle | 1 + .../sh/adb/RandomRedditMemesAPI/Main.java | 37 ++++--------------- .../adb/RandomRedditMemesAPI/RedditAPI.java | 31 +++++++++++++++- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/build.gradle b/build.gradle index 833fef7..15b4fee 100644 --- a/build.gradle +++ b/build.gradle @@ -9,6 +9,7 @@ dependencies { compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.3' compile group: 'org.apache.oltu.oauth2', name: 'org.apache.oltu.oauth2.client', version: '1.0.0' + compile group: 'javax.xml.bind', name : 'jaxb-api', version: '2.3.1' testCompile group: 'commons-validator', name: 'commons-validator', version: '1.4.1' testCompile group: 'junit', name: 'junit', version: '4.8.1' testCompile group: 'org.mockito', name: 'mockito-core', version: '1.9.5' diff --git a/src/main/sh/adb/RandomRedditMemesAPI/Main.java b/src/main/sh/adb/RandomRedditMemesAPI/Main.java index 05e2079..3b10022 100644 --- a/src/main/sh/adb/RandomRedditMemesAPI/Main.java +++ b/src/main/sh/adb/RandomRedditMemesAPI/Main.java @@ -1,45 +1,24 @@ package sh.adb.RandomRedditMemesAPI; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; +import com.github.jreddit.oauth.exception.RedditOAuthException; import com.sun.net.httpserver.HttpServer; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.net.InetSocketAddress; public class Main { - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, RedditOAuthException { HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("", new ErrorHandler()); //server.createContext("/api/v1/", new StandardHandler()); server.createContext("/api/v1/r/", new CommunityHandler()); server.setExecutor(null); server.start(); + + RedditAPI client = new RedditAPI(); + System.out.println("the token is"); + System.out.println(client.token); } } -class CommunityHandler implements HttpHandler { - @Override - public void handle(HttpExchange t) throws IOException { - InputStream is = t.getRequestBody(); - //is.read(); - String response = "This is the response"; - t.sendResponseHeaders(200, response.length()); - OutputStream os = t.getResponseBody(); - os.write(response.getBytes()); - os.close(); - } -} -class ErrorHandler implements HttpHandler { - @Override - public void handle(HttpExchange t) throws IOException { - InputStream is = t.getRequestBody(); - //is.read(); - String response = "404"; - t.sendResponseHeaders(404, response.length()); - OutputStream os = t.getResponseBody(); - os.write(response.getBytes()); - os.close(); - } -} + + diff --git a/src/main/sh/adb/RandomRedditMemesAPI/RedditAPI.java b/src/main/sh/adb/RandomRedditMemesAPI/RedditAPI.java index 3740705..3a4f84e 100644 --- a/src/main/sh/adb/RandomRedditMemesAPI/RedditAPI.java +++ b/src/main/sh/adb/RandomRedditMemesAPI/RedditAPI.java @@ -8,5 +8,34 @@ import com.github.jreddit.oauth.app.RedditApp; import com.github.jreddit.oauth.app.RedditInstalledApp; import com.github.jreddit.oauth.exception.RedditOAuthException; -public class RedditAPI { +public class RedditAPI { + private RedditApp redditApp; + private RedditOAuthAgent agent; + public RedditToken token; + + public RedditAPI() throws RedditOAuthException { + // Information about the app + String userAgent = "jReddit: Reddit API Wrapper for Java"; + String clientID = "gn9RWt8rgo2GDoZK"; + String redirectURI = "https://github.com/snkas/jReddit"; + + // Reddit application + RedditApp redditApp = new RedditInstalledApp(clientID, redirectURI); + + // Create OAuth agent + RedditOAuthAgent agent = new RedditOAuthAgent(userAgent, redditApp); + + // Create token (will be valid for 1 hour) + RedditToken token = agent.tokenAppOnly(false); + System.out.println("Access Token: " + token.getAccessToken()); + System.out.println("Token Type: " + token.getTokenType()); + System.out.println("Refreshable: " + token.isRefreshable()); + System.out.println("Expired: " + token.isExpired()); + System.out.println("Expiration: " + token.getExpiration()); + 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 { + RedditToken token = this.agent.tokenAppOnly(false); + } }