1
votes

I know this question have been asked a million times but I can't figure out what's wrong with my token request.

The command in curl is

curl -v -X POST -H "Authorization: Basic XXXXXXXXXXXXXXXXXXXXXX" -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" -k -d "grant_type=password&username=XXXXX&password=XXXXX" https://localhost/sso/token

Converted to jersey it should be:

Client client = Client.create();
WebResource webResource = client.resource("https://localhost/sso/token");
String appKey= "Basic  XXXXXXXXXXXXXXXXXXXXXX"
String input="grant_type=password&username=XXXXX&password=XXXXX";
    ClientResponse response = null; 
            response = webResource.
                    header("Authorization", appKey).
                    header("Content-Type", "/x-www-form-urlencoded;charset=UTF-8").
                    accept("application/json").
                    post(ClientResponse.class, input);




    if (response.getStatus() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : "
                            + response.getStatus());
                }
                String output = response.getEntity(String.class);
                System.out.println("Server response .... \n");
                System.out.println(output);
            } catch (Exception e) {
                e.printStackTrace();
        }**

The answer I get:

java.lang.RuntimeException: Failed : HTTP error code : 415 at com.javacodegeeks.enterprise.rest.jersey.jerseyclient.JerseyClientAccessToken.main(JerseyClientAccessToken.java:67)

Can someone please tell me what am I doing wrong ?

1

1 Answers

1
votes

This:

header("Content-Type", "application/json;charset=UTF-8");

is not the same of this:

Content-Type: "application/x-www-form-urlencoded;charset=UTF-8" 

So you should change it. That's why you are getting a 415 error (Unsupported Media Type).