I'm developing a small app to learn about request/response/gson/etc.
What i'm trying to do is: Get the token through Client Credential Flow, to get some audio features.
I followed the guide: https://developer.spotify.com/web-api/authorization-guide/#client-credentials-flow
And worked on the same idea as the spotify api: https://github.com/thelinmichael/spotify-web-api-java/blob/master/src/main/java/com/wrapper/spotify/SpotifyHttpManager.java
Tried diff's stuffs, but i'm stucked now.
Currently, the error is:
HttpResponseProxy{HTTP/1.1 415 Unsupported Media Type [Server: nginx, Date: Thu, 02 Mar 2017 19:59:45 GMT, Content-Length: 888, Connection: keep-alive, Keep-Alive: timeout=600] ResponseEntityProxy{[Content-Length: 888,Chunked: false]}}
Here's the actual code:
BASE64Encoder base64Encoder = new BASE64Encoder();
String encodedClientIdKey = base64Encoder.encode((clientId + ":" + clientSecretKey).getBytes());
HttpPost httpPostRequest = new HttpPost("https://accounts.spotify.com/api/token");
httpPostRequest.setHeader("Content-Type", "application/json");
httpPostRequest.addHeader("Authorization", "Basic " + encodedClientIdKey);
JsonObject json = new JsonObject();
json.addProperty("grant_type", "client_credentials");
StringEntity stringEntity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
httpPostRequest.setEntity(stringEntity);
HttpResponse post = httpClient.execute(httpPostRequest);
I thought it was a content type problem, but couldn't solve by setting it on header or entity.
Any ideas?
PS:
Tried using form urlencoded instead of json as content type, here's the code (tried add content on header then as parameter as well):
BASE64Encoder base64Encoder = new BASE64Encoder();
String encodedClientIdKey = base64Encoder.encode((clientId + ":" + clientSecretKey).getBytes());
HttpPost httpPostRequest = new HttpPost("https://accounts.spotify.com/api/token");
httpPostRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPostRequest.addHeader("Authorization", "Basic " + encodedClientIdKey);
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("grant_type", "client_credentials"));
nameValuePairs.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded"));
httpPostRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse post = httpClient.execute(httpPostRequest);
System.out.println(post);
And the error was Bad request this time:
HttpResponseProxy{HTTP/1.1 400 Bad Request [Server: nginx, Date: Thu, 02 Mar 2017 20:14:50 GMT, Content-Type: application/json, Content-Length: 70, Connection: keep-alive, Keep-Alive: timeout=600] ResponseEntityProxy{[Content-Type: application/json,Content-Length: 70,Chunked: false]}}