1

So I'm trying to get the access token for the Web API with the client_id and client_secret, but I'm keep getting this error: {"error":"unsupported_grant_type","error_description":"grant_type parameter is missing"} This is my code: `

public static void getAuthToken() throws Exception {
    Gson gson = new Gson();
    Transcript transcript = new Transcript();
    String jsonRequest = gson.toJson(transcript);
    String originalInput = Access.CLIENT_ID + ":" + Access.CLIENT_SECRET;
    String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
    System.out.println(jsonRequest);
    HttpRequest postRequest = (HttpRequest) HttpRequest.newBuilder()
            .uri(new URI("https://accounts.spotify.com/api/token"))
            .POST(HttpRequest.BodyPublishers.ofString(jsonRequest))
            .header("Authorization", "Basic " + encodedString)
            .header("Content-Type", "application/x-www-form-urlencoded")
            .build();

    HttpClient httpClient = HttpClient.newHttpClient();

    HttpResponse<String> postResponse = httpClient.send(postRequest, HttpResponse.BodyHandlers.ofString());
    System.out.println(postResponse.body());
}

and:

public class Transcript {

    private String grant_type = "client_credentials";

}

`

Well the error obviously states that something is wrong with the grant_type, so I printed the request body and it seems fine.

kalle
  • 11
  • 1
  • As I see u pass grant_type parameter as a request body not a form parameter(s). Try to build a valid x-www-form-url-encoded request. Hope it helps: https://stackoverflow.com/questions/56728398/java-11-new-http-client-send-post-requests-with-x-www-form-urlencoded-parameter – zforgo Nov 15 '22 at 13:14
  • You are passing the values as body but it requires them as form params. – Alex Karamfilov Nov 16 '22 at 15:52

0 Answers0