move handler + start reddit session
This commit is contained in:
parent
4b47d907cd
commit
db654ddfdd
@ -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'
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user