从spotify检索身份验证代码时出错:“localhost不在此站点密钥中支持的域的列表中”

kiayqfof  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(527)

我正在开发一个小的spring-boot应用程序,能够在检索身份验证代码后与用户的spotify帐户连接。然而,google的recaptcha不允许localhost作为有效域,并且似乎要求我用google的recaptcha注册我的设备,使用他们的密钥,而不是spotify给我的应用程序的密钥。我已经在spotify注册了localhost和重定向。
spotify的身份验证错误消息
不过,这可能与我如何实现spotify的请求有关。我使用以下函数呈现从spotify身份验证代码get请求正文中获取的html页面:

public String getAuthorizationCode() throws URISyntaxException {        
    // set query parameters
    Map<String, String> queryParams = new HashMap<>();
    queryParams.put("scope", authConfigs.getScope());
    queryParams.put("response_type", "code");
    queryParams.put("redirect_uri", authConfigs.getRedirectURI().toString());
    queryParams.put("client_id", authConfigs.getClientId());

    // hit request
    ResponseEntity<String> responseEntity = restCall(queryParams);

    String body = responseEntity.getBody();
    System.out.println(body);
    return body;
}

调用此函数:

ResponseEntity<String> restCall(Map<String, String> queryParams) {
    ResponseEntity<String> responseEntity;

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);
    HttpEntity<String> entity = new HttpEntity<String>(null, headers);
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://accounts.spotify.com/authorize/");
    queryParams.keySet().forEach(key -> builder.queryParam(key, queryParams.get(key)));
    RestTemplate restTemplate = new RestTemplate();
    responseEntity = restTemplate.exchange(builder.build(false).toUri(), HttpMethod.GET, entity, String.class);

    return responseEntity;
}

我使用@responsebody注解直接呈现给用户:

@RequestMapping("/request")
@ResponseBody
    @GetMapping
    public String getAuthorizationCode() throws URISyntaxException {
        System.out.println("Getting authorizationCode");
        //Document doc = Jsoup.connect(authorizationService.getAuthorizationCode().toString()).get();
        //doc
        String authCode = authorizationService.getAuthorizationCode();
        System.out.println("Auth Code is " + authCode);
        return authCode;
    }

我不确定这样做是否正确,或者是否需要将它们直接重定向到一个网页(我对spring和互联网协议一般来说都很陌生,所以这里可能有一些非常明显的东西)。如果我需要将用户重定向到另一个网页,我如何在 Spring 这样做?我一直在尝试使用重定向视图以及故障排除,但这是给我的错误。
谢谢您!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题