|
| 1 | +import com.github.copilot.sdk.CopilotClient; |
| 2 | +import com.github.copilot.sdk.json.CopilotClientOptions; |
| 3 | +import com.github.copilot.sdk.json.MessageOptions; |
| 4 | +import com.github.copilot.sdk.json.SessionConfig; |
| 5 | + |
| 6 | +import java.net.URI; |
| 7 | +import java.net.http.HttpClient; |
| 8 | +import java.net.http.HttpRequest; |
| 9 | +import java.net.http.HttpResponse; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.JsonNode; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | + |
| 14 | +public class Main { |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + var clientId = System.getenv("GITHUB_OAUTH_CLIENT_ID"); |
| 17 | + if (clientId == null || clientId.isEmpty()) { |
| 18 | + System.err.println("Missing GITHUB_OAUTH_CLIENT_ID"); |
| 19 | + System.exit(1); |
| 20 | + } |
| 21 | + |
| 22 | + var mapper = new ObjectMapper(); |
| 23 | + var httpClient = HttpClient.newHttpClient(); |
| 24 | + |
| 25 | + // Step 1: Request device code |
| 26 | + var deviceCodeReq = HttpRequest.newBuilder() |
| 27 | + .uri(URI.create("https://github.com/login/device/code")) |
| 28 | + .header("Accept", "application/json") |
| 29 | + .header("User-Agent", "copilot-sdk-java") |
| 30 | + .POST(HttpRequest.BodyPublishers.ofString("client_id=" + clientId)) |
| 31 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 32 | + .build(); |
| 33 | + var deviceCodeResp = httpClient.send(deviceCodeReq, HttpResponse.BodyHandlers.ofString()); |
| 34 | + var deviceCode = mapper.readTree(deviceCodeResp.body()); |
| 35 | + |
| 36 | + var userCode = deviceCode.get("user_code").asText(); |
| 37 | + var verificationUri = deviceCode.get("verification_uri").asText(); |
| 38 | + var code = deviceCode.get("device_code").asText(); |
| 39 | + var interval = deviceCode.get("interval").asInt(); |
| 40 | + |
| 41 | + System.out.println("Please visit: " + verificationUri); |
| 42 | + System.out.println("Enter code: " + userCode); |
| 43 | + |
| 44 | + // Step 2: Poll for access token |
| 45 | + String accessToken = null; |
| 46 | + while (accessToken == null) { |
| 47 | + Thread.sleep(interval * 1000L); |
| 48 | + var tokenReq = HttpRequest.newBuilder() |
| 49 | + .uri(URI.create("https://github.com/login/oauth/access_token")) |
| 50 | + .header("Accept", "application/json") |
| 51 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 52 | + .POST(HttpRequest.BodyPublishers.ofString( |
| 53 | + "client_id=" + clientId |
| 54 | + + "&device_code=" + code |
| 55 | + + "&grant_type=urn:ietf:params:oauth:grant-type:device_code")) |
| 56 | + .build(); |
| 57 | + var tokenResp = httpClient.send(tokenReq, HttpResponse.BodyHandlers.ofString()); |
| 58 | + var tokenData = mapper.readTree(tokenResp.body()); |
| 59 | + |
| 60 | + if (tokenData.has("access_token")) { |
| 61 | + accessToken = tokenData.get("access_token").asText(); |
| 62 | + } else if (tokenData.has("error")) { |
| 63 | + var err = tokenData.get("error").asText(); |
| 64 | + if ("authorization_pending".equals(err)) continue; |
| 65 | + if ("slow_down".equals(err)) { interval += 5; continue; } |
| 66 | + throw new RuntimeException("OAuth error: " + err); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Step 3: Verify authentication |
| 71 | + var userReq = HttpRequest.newBuilder() |
| 72 | + .uri(URI.create("https://api.github.com/user")) |
| 73 | + .header("Authorization", "Bearer " + accessToken) |
| 74 | + .header("User-Agent", "copilot-sdk-java") |
| 75 | + .GET() |
| 76 | + .build(); |
| 77 | + var userResp = httpClient.send(userReq, HttpResponse.BodyHandlers.ofString()); |
| 78 | + var userData = mapper.readTree(userResp.body()); |
| 79 | + System.out.println("Authenticated as: " + userData.get("login").asText()); |
| 80 | + |
| 81 | + // Step 4: Use the token with Copilot |
| 82 | + try (var client = new CopilotClient(new CopilotClientOptions() |
| 83 | + .setGitHubToken(accessToken))) { |
| 84 | + client.start().get(); |
| 85 | + var session = client.createSession( |
| 86 | + new SessionConfig() |
| 87 | + .setModel("claude-haiku-4.5")) |
| 88 | + .get(); |
| 89 | + var response = session.sendAndWait( |
| 90 | + new MessageOptions().setPrompt("What is the capital of France?")) |
| 91 | + .get(); |
| 92 | + if (response != null) { |
| 93 | + System.out.println(response.getData().content()); |
| 94 | + } |
| 95 | + client.stop().get(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments