如何修复在Spring应用程序中应用GitHub OAuth时的“Whitelabel Error Page”?

rvpgvaaj  于 2023-06-05  发布在  Spring
关注(0)|答案(1)|浏览(179)

在我的spring应用程序中尝试应用Github OAuth时获取Whitelabel Error Page这个应用程序没有明确的/errorMap,所以你可以把它看作是一个后备。
发生了一个意外的错误(类型=内部服务器错误,状态=500)。由于在[Source:(String)"";行:1,列:0]
下面是我的java代码,我试图打印或返回userDataResult.body,但在登录后出现错误:

@RestController
public class LoginController {
    private String clientSecret = "541a3c6bdca41a233d593ed263f628915317c434";
    private String clientId = "f3cc203f5df4b2e30d5c";

    @GetMapping("/auth")
    public ResponseEntity<String> auth(@RequestBody String code) {
        String accessTokenString = String.format(
                "https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", clientId,
                clientSecret, code);

        HttpClient client = HttpClient.newHttpClient();
        URI accessTokenUri = URI.create(accessTokenString);
        HttpRequest.Builder accessTokenBuilder = HttpRequest.newBuilder().uri(accessTokenUri);
        HttpRequest accessTokenRequest = accessTokenBuilder.POST(HttpRequest.BodyPublishers.noBody())
                .header("Accept", "Application/json").build();

        HttpResponse<String> accessTokenResult = null;
        try {
            accessTokenResult = client.send(accessTokenRequest, HttpResponse.BodyHandlers.ofString());
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        ObjectMapper mapper = new ObjectMapper();
        HashMap<String, Object> resultBody = new HashMap<>();
        try {
            resultBody = mapper.readValue(accessTokenResult.body(), HashMap.class);
        } catch (JsonProcessingException e1) {
            e1.printStackTrace();
        }
        String accessToken = (String) resultBody.get("access_token");

        URI userDataUri = URI.create("https://api.github.com/user");
        HttpRequest.Builder userDataBuilder = HttpRequest.newBuilder().uri(userDataUri);
        HttpRequest req = userDataBuilder.GET().header("Authorization", String.format("token %s", accessToken)).build();
        try {
            HttpResponse<String> userDataResult = client.send(req, HttpResponse.BodyHandlers.ofString());
            GithubUser user = mapper.readValue(userDataResult.body(), GithubUser.class);
            System.out.println(userDataResult.body());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

也写了一个html来检查:

<a href="https://github.com/login/oauth/authorize?client_id=f3cc203f5df4b2e30d5c&scope=user"><h1>Click to login using github</h1></a>

已经尝试检查令牌是否为空,我不知道为什么它应该是空的,还尝试打印accessTokenResult.body()

0x6upsns

0x6upsns1#

修改代码,这样就可以了:

@GetMapping(value = "/auth", produces = MediaType.APPLICATION_JSON_VALUE)
    public String auth(@RequestParam(value = "code") String code) throws Exception {
        String accessTokenURL = String.format(
                "https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s",
                clientId, clientSecret, code
        );
        HttpClient client = HttpClient.newHttpClient();
        URI accessTokenURI = URI.create(accessTokenURL);
        HttpRequest.Builder accessTokenBuilder = HttpRequest.newBuilder().uri(accessTokenURI);
        HttpRequest accessTokenRequest = accessTokenBuilder.POST(HttpRequest.BodyPublishers.noBody())
                .header("Accept", "application/json").build();
        HttpResponse<String> accessTokenResult = client.send(accessTokenRequest, HttpResponse.BodyHandlers.ofString());
        ObjectMapper mapper = new ObjectMapper();
        HashMap<String, Object> resultBody = mapper.readValue(accessTokenResult.body(), HashMap.class);
        String accessToken = (String) resultBody.get("access_token");
        URI userDateURI = URI.create("https://api.github.com/user");
        HttpRequest.Builder userDataBuilder = HttpRequest.newBuilder().uri(userDateURI);
        HttpRequest req = userDataBuilder.GET().header("Authorization", String.format("token %s", accessToken)).build();
        HttpResponse<String> userDataResult = client.send(req, HttpResponse.BodyHandlers.ofString());
        HashMap<String, Object> userData = mapper.readValue(userDataResult.body(), HashMap.class);
        return userDataResult.body();
    }

相关问题