我想在 Spotify 数据库中进行一些搜索,仅用于学习 REST 应用程序的目的。 我陷入了第一点 https://developer.spotify.com/documentation/general/guides/authorization-guide/ 授权码流程 使用 java 应用程序登录 Spotify:
package spotify;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/*
1) https://developer.spotify.com/dashboard/applications
*/
public class SpotifyMain {
static final String CLIENTID = "blablablablablablablabla";
static final String CLIENTSECRET = "blablablablablablablabla";
static final String REDIRECTURL = "https://github.com/blablablablablablablabla"; //whiltelisted set inside spotify
public static void main(String[] args) {
try {
String url_auth =
"https://accounts.spotify.com/authorize?"
+ "client_id="+CLIENTID+"&"
+ "response_type=code&"
+ "redirect_uri="+REDIRECTURL+"&"
+ "scope=user-read-private%20user-read-email&"
+ "state=34fFs29kd09";
System.out.println(url_auth);
URL url = new URL(url_auth);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我收到错误 406。 我认为该网址是正确的,因为如果我将该网址放入浏览器中,它会正确显示该应用程序想要访问某些资源。 如果我将该 url 放入 postman 中,错误会更改为 400 bad request... 我错过了什么吗? 谢谢
最佳答案
尝试删除此行:
conn.setRequestProperty("Accept", "application/json");
HTTP 406 错误为“ Not Acceptable ”,这意味着服务器已生成无法作为请求的 Accept 内容类型返回的回复。
关于java - 使用 java 的 Spotify Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59837044/