0

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]}}
fsakiyama
  • 337
  • 3
  • 13
  • Why don't you make it easier for your self, and just include the authorization as parameter in your HTTP Post request – johnny 5 Mar 02 '17 at 19:11
  • You mean remove this line: httpPostRequest.addHeader("Authorization", "Basic " + encodedClientIdKey); and put the Authorization on StringEntity? – fsakiyama Mar 02 '17 at 19:31
  • 1. Why are you passing your authorization from your server and not the client? 2. Most oAuth implementations use `x-www-form-urlencoded` content mean you need to change your encoding of your post body 3. There is a class from the server side TokenClient that you can use from IdentityServer if you want to extract it from there instead of building your own requests – johnny 5 Mar 02 '17 at 19:36
  • I'm following spotify-api to do post, but i wanted to build my own requests for learning purposes. Here's the class in the api which have the post method: [Github Link](https://github.com/thelinmichael/spotify-web-api-java/blob/master/src/main/java/com/wrapper/spotify/SpotifyHttpManager.java) He uses json instead of x-www-form-urlencoded. Forgot to mention the api is using apache http 3.1 while i'am using 4.5.3. – fsakiyama Mar 02 '17 at 19:50
  • What error messages are you getting? – johnny 5 Mar 02 '17 at 19:54
  • Im sorry, forgot to add. Edited the post, but its Unsupported media. – fsakiyama Mar 02 '17 at 20:03
  • According to this [Stack](http://stackoverflow.com/questions/25432678/415-error-when-querying-spotify-for-tokens), you need to use x-www-form-urlencoding, I did look at the clas you showed me before which used application json, but they still seems to encode the url parameters anyway – johnny 5 Mar 02 '17 at 20:06
  • Hmm i tried that way, just edited the post with the altered code to use form urlencoded, and got bad request this time. Not sure what that could be.. – fsakiyama Mar 02 '17 at 20:18
  • Try that with out the authorization attribute and adding the client Id and secret as parameters, are you sure your post body is URL encoded – johnny 5 Mar 02 '17 at 20:24
  • Thx for the help, back again! I'm 99% sure that the body is url encoded because of this: new UrlEncodedFormEntity(nameValuePairs). And about adding client id and secret as parameters, i don't know how it would work, since spotify api reads it from header and on auth format. – fsakiyama Mar 03 '17 at 11:49
  • Why don't you try connecting via postman or fiddler, once that works convert the request to code – johnny 5 Mar 03 '17 at 13:46
  • I tried with postman first, many times and ways, but couldn't solve. Do you mind telling me a scenario that might work? I tried with: https://accounts.spotify.com/api/token?grant_type=client_credentials and passing auth on header, key field being Authorization, and value field being Basic clientId:secretKey (omiting real id values, but i tried with base64encoded and not encoded) – fsakiyama Mar 03 '17 at 14:26

0 Answers0