added TwitchOauth.getToken

This commit is contained in:
DaniTheSkunk 2022-11-28 22:28:34 +00:00
parent 4f08c392c5
commit 4487a03238
3 changed files with 138 additions and 10 deletions

View File

@ -2,7 +2,6 @@
<code_scheme name="Project" version="173">
<option name="AUTODETECT_INDENTS" value="false" />
<option name="RIGHT_MARGIN" value="80" />
<option name="WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN" value="true" />
<option name="ENABLE_SECOND_REFORMAT" value="true" />
<JavaCodeStyleSettings>
<option name="ANNOTATION_PARAMETER_WRAP" value="5" />
@ -36,7 +35,6 @@
<option name="ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE" value="true" />
<option name="WHILE_BRACE_FORCE" value="3" />
<option name="FOR_BRACE_FORCE" value="3" />
<option name="WRAP_LONG_LINES" value="true" />
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="USE_TAB_CHARACTER" value="true" />

View File

@ -1,16 +1,13 @@
package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.audio.Samplei;
import com.danitheskunk.skunkworks.gfx.Color;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.vt.Terminal;
import marytts.LocalMaryInterface;
import marytts.exceptions.MaryConfigurationException;
import marytts.exceptions.SynthesisException;
import marytts.util.data.audio.MaryAudioUtils;
import com.danitheskunk.skunkworks.net.TwitchOauth;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import javax.sound.sampled.AudioInputStream;
import java.util.concurrent.atomic.AtomicReference;
import java.io.IOException;
public class Test extends BaseGame {
private Terminal term;
@ -18,8 +15,12 @@ public class Test extends BaseGame {
public Test() {
super(new Vec2i(40 * 12, 22 * 12), "Skunkworks");
tts.say("Welcome to the world of speech synthesis!");
var clientID = "thvs1beyj8w6ono2jqirdz7uuu62qu";
var token = TwitchOauth.getToken(
clientID,
new String[]{"chat:read", "chat:write"}
);
var path = "C:\\Program Files (x86)" +
@ -86,4 +87,41 @@ public class Test extends BaseGame {
System.out.println("right click");
}
}
static class TokenHandler implements HttpHandler {
static String HTML = """
<html><body><script>
if(document.location.hash) {
window.location.href = "http://localhost:42069/?" +
document.location.hash.slice(1);
} else {
window.close();
}
</script></body></html>
""";
public boolean errored = false;
public String result = null;
@Override
public void handle(HttpExchange exchange) throws IOException {
var query = exchange.getRequestURI().getQuery();
exchange.sendResponseHeaders(200, HTML.length());
var response = exchange.getResponseBody();
response.write(HTML.getBytes());
response.close();
if(query != null) {
var params = query.split("&");
for(var param : params) {
var p = param.split("=");
if(p[0].equals("access_token")) {
result = p[1];
break;
}
}
if(result == null) {
errored = true;
}
}
}
}
}

View File

@ -0,0 +1,92 @@
package com.danitheskunk.skunkworks.net;
import com.danitheskunk.skunkworks.Test;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.awt.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Arrays;
public class TwitchOauth implements HttpHandler {
static String HTML = """
<html><body><script>
if(document.location.hash) {
window.location.href = "http://localhost:42069/?" +
document.location.hash.slice(1);
} else {
window.close();
}
</script></body></html>
""";
private boolean errored = false;
private String result = null;
public static String getToken(String clientID, String[] scopes) {
var scopeStr = String.join("&",
Arrays.stream(scopes).map((String s) -> URLEncoder.encode(s)).toList()
);
var uri = URI.create(String.format(
"https://id.twitch.tv/oauth2/authorize?client_id=%s&redirect_uri=http%%3A%%2F%%2Flocalhost%%3A42069&response_type=token&scope=%s",
clientID,
scopeStr
));
HttpServer server;
try {
server = HttpServer.create(new InetSocketAddress(42069), 0);
} catch(IOException e) {
throw new RuntimeException(e);
}
var instance = new TwitchOauth();
server.createContext("/", instance);
server.setExecutor(null);
server.start();
try {
Desktop.getDesktop().browse(uri);
} catch(IOException e) {
throw new RuntimeException(e);
}
while(instance.result == null && !instance.errored) {
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("waiting for oauth response");
}
server.stop(0);
return instance.result;
}
@Override
public void handle(HttpExchange exchange) throws IOException {
var query = exchange.getRequestURI().getQuery();
exchange.sendResponseHeaders(200, HTML.length());
var response = exchange.getResponseBody();
response.write(HTML.getBytes());
response.close();
if(query != null) {
var params = query.split("&");
for(var param : params) {
var p = param.split("=");
if(p[0].equals("access_token")) {
result = p[1];
break;
}
}
if(result == null) {
errored = true;
}
}
}
}