From 6345f5d70b43557dc1a22966c2a64d28c91a975d Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Wed, 16 Jul 2025 11:05:58 +0800 Subject: [PATCH] Handle GitHub email API errors --- .../openisle/service/GithubAuthService.java | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/openisle/service/GithubAuthService.java b/src/main/java/com/openisle/service/GithubAuthService.java index 9f26c51c7..58a0f802b 100644 --- a/src/main/java/com/openisle/service/GithubAuthService.java +++ b/src/main/java/com/openisle/service/GithubAuthService.java @@ -9,8 +9,11 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import org.springframework.web.client.HttpClientErrorException; import java.util.Collections; +import java.util.Map; +import java.util.HashMap; import java.util.Optional; @Service @@ -30,12 +33,17 @@ public class GithubAuthService { String tokenUrl = "https://github.com/login/oauth/access_token"; HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - HttpEntity request = new HttpEntity<>( - "client_id=" + clientId + - "&client_secret=" + clientSecret + - "&code=" + code + - (redirectUri != null ? "&redirect_uri=" + redirectUri : ""), - headers); + headers.setContentType(MediaType.APPLICATION_JSON); + + Map body = new HashMap<>(); + body.put("client_id", clientId); + body.put("client_secret", clientSecret); + body.put("code", code); + if (redirectUri != null) { + body.put("redirect_uri", redirectUri); + } + + HttpEntity> request = new HttpEntity<>(body, headers); ResponseEntity tokenRes = restTemplate.postForEntity(tokenUrl, request, JsonNode.class); if (!tokenRes.getStatusCode().is2xxSuccessful() || tokenRes.getBody() == null || !tokenRes.getBody().has("access_token")) { return Optional.empty(); @@ -43,6 +51,7 @@ public class GithubAuthService { String accessToken = tokenRes.getBody().get("access_token").asText(); HttpHeaders authHeaders = new HttpHeaders(); authHeaders.setBearerAuth(accessToken); + authHeaders.set(HttpHeaders.USER_AGENT, "OpenIsle"); HttpEntity entity = new HttpEntity<>(authHeaders); ResponseEntity userRes = restTemplate.exchange( "https://api.github.com/user", HttpMethod.GET, entity, JsonNode.class); @@ -56,15 +65,19 @@ public class GithubAuthService { email = userNode.get("email").asText(); } if (email == null || email.isEmpty()) { - ResponseEntity emailsRes = restTemplate.exchange( - "https://api.github.com/user/emails", HttpMethod.GET, entity, JsonNode.class); - if (emailsRes.getStatusCode().is2xxSuccessful() && emailsRes.getBody() != null && emailsRes.getBody().isArray()) { - for (JsonNode n : emailsRes.getBody()) { - if (n.has("primary") && n.get("primary").asBoolean()) { - email = n.get("email").asText(); - break; + try { + ResponseEntity emailsRes = restTemplate.exchange( + "https://api.github.com/user/emails", HttpMethod.GET, entity, JsonNode.class); + if (emailsRes.getStatusCode().is2xxSuccessful() && emailsRes.getBody() != null && emailsRes.getBody().isArray()) { + for (JsonNode n : emailsRes.getBody()) { + if (n.has("primary") && n.get("primary").asBoolean()) { + email = n.get("email").asText(); + break; + } } } + } catch (HttpClientErrorException ignored) { + // ignore when the email API is not accessible } } if (email == null) {