使用Spotify API-如何在Java中遵循多个重定向链接



我正在做一个使用Spotify Web Api的Hyperskill项目。我正在使用Spotify Api Wrapper库(https://github.com/thelinmichael/spotify-web-api-java)。我正在使用授权代码流(https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-代码流(。这意味着,当用户授予访问权时,Spotify会使用访问令牌将我重定向到localhost。长话短说,我能够用ServerSocket创建一个localhost服务器,我可以让用户授予或拒绝对Spotify的访问,它完美地在localhost链接中为我生成了访问令牌。我的问题是,我无法从回调链接获取此访问令牌/代码(https://example.com/callback?code=NApCCg..BkWtQ&state=配置文件%2Activity(。我知道我必须遵循重定向,我的猜测是Spotify进行了不止一次重定向,因为我的代码输出如下:

https://accounts.spotify.com/login?continue=https%3A%2F%2Faccounts.spotify.com%2Fauthorize%3Fclient_id%123EXAMPLECODE%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A8080%26response_type%3Dcode

URL url = new URL("https://accounts.spotify.com/authorize?client_id=123EXAMPLECODE&redirect_uri=http://localhost:8080&response_type=code");
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
URLConnection conn = secondURL.openConnection();
System.out.println(secondURL);

您可以看到它将我从"authorize"重定向到"login",然后返回到localhost,我可以按照重定向到login。如何使其跟随localhost?或者有什么方法可以从回调中获取这些代码吗?

如果有任何帮助,我将不胜感激!

我使用HttpExchange的getQuery((方法从回调链接中获取了令牌。请认为这个问题已经解决了。

最新更新