Java HttpsURLConnection - Spotify 授权 API
Java HttpsURLConnection - Authorization with Spotify API
几天来我一直在努力让它发挥作用。
我正在尝试使用 Java 使用客户端凭据授权流程向 Spotify 进行授权。但是,尽管有 Content-Length 属性.
,我仍然收到 411 Length Required HTTP 错误
HttpsURLConnection spotifyAccounts = null;
try {
spotifyAccounts = (HttpsURLConnection) new URL("https://accounts.spotify.com/api/token?grant_type=client_credentials").openConnection();
spotifyAccounts.setDoOutput(true);
spotifyAccounts.setDoInput(true);
spotifyAccounts.setRequestMethod("POST");
String unencodedAuthorizationKey = "Basic " + "42f48d16044a4c79b5168c5784889827" + ":" + "d6bb7a72e99342319e6b6d8c6fd3cedd";
String encodedKey = Base64.getEncoder().encodeToString(unencodedAuthorizationKey.getBytes());
spotifyAccounts.setRequestProperty("Authorization", encodedKey);
spotifyAccounts.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
spotifyAccounts.setRequestProperty("User-Agent", "Mozilla/5.0");
int totalLength = 0;
for(Entry<String, List<String>> entry : spotifyAccounts.getRequestProperties().entrySet()) {
for(String string : entry.getValue()) {
totalLength += string.getBytes().length;
}
}
spotifyAccounts.setRequestProperty("Content-Length", String.valueOf(totalLength));
spotifyAccounts.connect();
} catch(IOException ex) {}
每次遇到411错误,我都会输出Content-Length请求属性的值,像这样:
int code = 0;
try {
code = spotifyAccounts.getResponseCode();
} catch (IOException ex) {
ex.printStackTrace();
}
if(code != 200) {
if(code == 411) {
System.out.println("Spotify Accounts - Content-Length: " + spotifyAccounts.getContentLength());
System.out.println("Spotify Accounts - Connecting User Agent: " + spotifyAccounts.getRequestProperty("User-Agent"));
}
// Other error code handling
}
输出 Content-Length 总是 return 值 1564。
我尝试了很多方法,例如将 HttpsURLConnection 更改为 HttpURLConnection,更改 Content-Type header,并直接将 Content-Length 设置为 0。但是所有这些解决方案仍然return 411 要求长度错误。还有什么我想念的,还是我误解了什么?
Spotify 文档说您需要在请求正文中设置 grant_type
,但是 ?grant_type=client_credentials
这不起作用。
相反,您可以像这样提交正文:
byte[] input = "grant_type=client_credentials".getBytes(StandardCharsets.UTF_8);
try (OutputStream os = connection.getOutputStream()) {
os.write(input);
}
几天来我一直在努力让它发挥作用。 我正在尝试使用 Java 使用客户端凭据授权流程向 Spotify 进行授权。但是,尽管有 Content-Length 属性.
,我仍然收到 411 Length Required HTTP 错误HttpsURLConnection spotifyAccounts = null;
try {
spotifyAccounts = (HttpsURLConnection) new URL("https://accounts.spotify.com/api/token?grant_type=client_credentials").openConnection();
spotifyAccounts.setDoOutput(true);
spotifyAccounts.setDoInput(true);
spotifyAccounts.setRequestMethod("POST");
String unencodedAuthorizationKey = "Basic " + "42f48d16044a4c79b5168c5784889827" + ":" + "d6bb7a72e99342319e6b6d8c6fd3cedd";
String encodedKey = Base64.getEncoder().encodeToString(unencodedAuthorizationKey.getBytes());
spotifyAccounts.setRequestProperty("Authorization", encodedKey);
spotifyAccounts.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
spotifyAccounts.setRequestProperty("User-Agent", "Mozilla/5.0");
int totalLength = 0;
for(Entry<String, List<String>> entry : spotifyAccounts.getRequestProperties().entrySet()) {
for(String string : entry.getValue()) {
totalLength += string.getBytes().length;
}
}
spotifyAccounts.setRequestProperty("Content-Length", String.valueOf(totalLength));
spotifyAccounts.connect();
} catch(IOException ex) {}
每次遇到411错误,我都会输出Content-Length请求属性的值,像这样:
int code = 0;
try {
code = spotifyAccounts.getResponseCode();
} catch (IOException ex) {
ex.printStackTrace();
}
if(code != 200) {
if(code == 411) {
System.out.println("Spotify Accounts - Content-Length: " + spotifyAccounts.getContentLength());
System.out.println("Spotify Accounts - Connecting User Agent: " + spotifyAccounts.getRequestProperty("User-Agent"));
}
// Other error code handling
}
输出 Content-Length 总是 return 值 1564。
我尝试了很多方法,例如将 HttpsURLConnection 更改为 HttpURLConnection,更改 Content-Type header,并直接将 Content-Length 设置为 0。但是所有这些解决方案仍然return 411 要求长度错误。还有什么我想念的,还是我误解了什么?
Spotify 文档说您需要在请求正文中设置 grant_type
,但是 ?grant_type=client_credentials
这不起作用。
相反,您可以像这样提交正文:
byte[] input = "grant_type=client_credentials".getBytes(StandardCharsets.UTF_8);
try (OutputStream os = connection.getOutputStream()) {
os.write(input);
}