I am trying to request an access token from the Spotify api following the Client Credentials Flow but I'm receiving the error 400 (bad request. This indicates that I probably have an error in my POST syntax but I can't figure it out. Can someone help me find what's wrong with my code?
import processing.net.*;
String baseURL = "accounts.spotify.com"; //Having "https://" here --> UnknownHostException
String endPoint = "/api/token";
//Base64 encoded client_ID:client_secret, encoded using https://www.base64encode.org/
String clientIDKey = "ZmQ3MDc2Y2JiMDk3NDlhOTk4ZmZhNDZjY2VlZDRiNmM6ZmNjZGZmYzA1ZDE2NDlkMGJjYTk0MDMyMjdiOWU2NGY=";
Client test;
void setup() {
test = new Client(this, baseURL, 80);
test.write("POST " + baseURL + endPoint + " HTTP/1.1\r\n"); //without baseURL --> 301 Moved Permanently
test.write("Host: " + baseURL + "\r\n");
test.write("Authorization: Basic " + clientIDKey + "\r\n");
test.write("Content-Type: application/x-www-form-urlencoded\r\n");
test.write("\r\n");
test.write("grant_type=client_credentials\r\n");
}
void draw() {
if (test.available() > 0) {
System.out.println(test.readString());
}
}