I'm building a little application and i need to use the spoify API, when I try to login I have
400 ERROR:{"error":"unsupported_grant_type","error_description":"grant_type parameter is missing"}
when I make the POST request
String urlString = "https://accounts.spotify.com/api/token?";
URL website = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) website.openConnection();
connection.setRequestMethod("POST");
// Headers
String toEncode = Test.CLIENT_ID + ":" + Test.CLIEN_SECRET_ID;
String encodedString = Base64.getEncoder().encodeToString(toEncode.getBytes());
connection.setRequestProperty("Authorization", encodedString);//client id + secret
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
// Add parameters to the body
String jsonInputString = "{" +
"grant_type: authorization_code,\n" +
"\"redirect_uri\": \"" + Test.REDIRECT_URI + "\",\n" +
"\"code\": \"" + code +
"\"\n}";
System.out.println("json input string " +jsonInputString);
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
connection.setFixedLengthStreamingMode(input.length);
connection.connect();
try (OutputStream os = connection.getOutputStream()) {
os.write(input);
}
I read a lot about this error but I cannot find who to correct it, I also asked other people to look at my code and check if I did something wrong they don't find it either.
Please tell me you see what's wrong and how I can correct it