I am trying to get some authorisation codes from Spotify. I use the athorisation flow described here: https://developer.spotify.com/web-api/authorization-guide/#authorization-code-flow. Now I am stuck at number four: "Your application requests refresh and access tokens" I have to ask Spotify for accses_token and refresh_token with an cURL request. The cURL request has to be something like this:
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
I tryed to achieve this with :
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);
}
I am just getting this Exception:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://accounts.spotify.com/api/token
Does someone see what I am doing wrong?