我正在尝试从Spotify获取一些授权码。我使用此处描述的主题化流程:https://developer.spotify.com/web-api/authorization-guide/#authorization-code-flow。现在我被困在第四位:"你的应用程序请求刷新和访问令牌"我必须通过cURL请求向Spotify请求accses_token和refresh_token。 cURL请求必须是这样的:
curl -H "Authorization: Basic ZjM...zE=" -d grant_type=authorization_code -d code=MQCbtKe...44KN -d redirect_uri=http://localhost/codeAuslesen.php https://accounts.spotify.com/api/token
我试图通过以下方式实现这一目标:
String clientId = <client_Id>;
String clientSecret = <clientSecret>;
String getTokenUrl = "https://accounts.spotify.com/api/token";
String encodeString = clientId + ":" + clientSecret;
String basicAuth = "Basic " + Base64.getEncoder().encode(encodeString.getBytes());
String url = getTokenUrl;
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", basicAuth);
conn.setRequestProperty("grant_type", "authorization_code");
conn.setRequestProperty("code", code);
conn.setRequestProperty("redirect_uri", redirectUri);
try {
BufferedReader tokenReader = new BufferedReader(new InputStreamReader(conn.getInputStream())); //Exception points here
while((tokenLine = tokenReader.readLine()) != null) {
tokenResult += tokenLine;
}
tokenReader.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(tokenResult);
}
我刚刚得到这个例外:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://accounts.spotify.com/api/token
有人看到我做错了吗?