Response code 400: grant_type parameter is missing

Reply
Highlighted

Response code 400: grant_type parameter is missing

Casual Listener

Plan: Premium

Country: USA

Device: Odroid H2

Operating System: Linux 19.10

 

My Question or Issue

I am working on a Java client. I have been able to access the whole API with no problem with the ironic exception of authorization. I would be quite happy if you could point me to any working Java example of getting the client_credentials token. The message I am currently getting and source code I am using are below:

Bad Request
400: Bad Request
{"error":"unsupported_grant_type","error_description":"grant_type parameter is missing"}
<<< Finished >>>

public static void main(final String[] args) {

getToken();

System.out.println("<<< Finished >>>");

}

private static final String CHARSET = "UTF-8";
private static final String GRANT_TYPE = "client_credentials";

public static String getToken() {

String PAYLOAD;
try {
// PAYLOAD = String.format("client_id=%s&client_secret=%s&grant_type=%s",
// URLEncoder.encode(CLIENT_ID, CHARSET),
// URLEncoder.encode(CLIENT_SECRET, CHARSET),
// URLEncoder.encode(GRANT_TYPE, CHARSET));
PAYLOAD = "grant_type=" + URLEncoder.encode("client_credentials", "UTF-8");
} catch (final UnsupportedEncodingException x) {
PAYLOAD = "";
x.printStackTrace();
}

String response = "";
try {
final URL url = new URL("https://accounts.spotify.com/api/token");
try {
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("grant_type", "client_credentials");
connection.setRequestProperty("Authorization", "Basic " + CLIENT_ID + ':' + CLIENT_SECRET);
connection.setRequestProperty("Content-Length", "" + PAYLOAD.length());
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);

try (final DataOutputStream writer = new DataOutputStream(connection.getOutputStream())) {
writer.writeChars("PAYLOAD");
}

if (connection.getResponseCode() == 200) {
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
while (reader.ready()) {
System.out.println(reader.readLine());
}
}
} else {
System.out.println(connection.getResponseMessage());
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
while (reader.ready()) {
System.out.println(connection.getResponseCode() + ": " + connection.getResponseMessage());
System.out.println(reader.readLine());
}
}
}

} catch (final IOException x) {
x.printStackTrace();
}
} catch (final MalformedURLException x) {
x.printStackTrace();
}

return response;

}

 

 

 

4 Replies
Highlighted

Re: Response code 400: grant_type parameter is missing

Rock Star 16
Rock Star 16

Hey @EdDowgiallo, I might be able to help here!

 

Hmm, I did some digging and I just wanted to check that the Authorization is passed in the Header of your request and that the grant_type is passed in the body of your POST request (encoded as application/x-www-form-urlencoded). Let me know if that helped here!

 

Have a good one,

Hubo

HuboRock Star 16
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.
Highlighted

Re: Response code 400: grant_type parameter is missing

Casual Listener
Sorry Hubo, but that is the way it is currently encoded and it does not work.
Do you have any example code for Java? If we get this working I would be happy to provide you with sample Java code for the entire API.
Ed
Highlighted

Re: Response code 400: grant_type parameter is missing

Casual Listener

Is there any other information I can provide to help resolve this issue?

Highlighted

Re: Response code 400: grant_type parameter is missing

Casual Listener

Do you have a working example using Java? That would get me going.

 

Ed

SUGGESTED POSTS