Automatically try to copy the access token to the clipboard

This commit is contained in:
Andreas Svanberg 2024-06-12 13:33:48 +02:00
parent 436e004e10
commit 77d5fec76e

@ -1,6 +1,8 @@
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Authenticator;
@ -15,6 +17,8 @@ import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class GetToken {
@ -55,6 +59,18 @@ public class GetToken {
try {
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
// Try to copy the access token to the clipboard
Matcher matcher = Pattern.compile("access_token\":\"([^\"]+)\"")
.matcher(response.body());
if (matcher.find()) {
StringSelection clipboardData = new StringSelection(matcher.group(1));
Toolkit.getDefaultToolkit()
.getSystemClipboard()
.setContents(clipboardData, clipboardData);
try { Thread.sleep(1_000L); } catch (InterruptedException e) { }
System.out.println("Access token copied to clipboard (probably)");
}
} catch (Exception e) {
e.printStackTrace();
} finally {