简体   繁体   中英

Post Request using resttemplate but have 401 unauthorised

String url = "https://api.assembla.com/token?";

RestTemplate restTemplate = new RestTemplate();

MultiValueMap<String, String> body = 
        new LinkedMultiValueMap<String, String>();

body.add("client_id", "myid");
body.add("client_secret", "mysecret");
body.add("grant_type", "client_credentials");
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-type", "application/json");
HttpEntity<?> entity = new HttpEntity<Object>(body, headers);

ResponseEntity<JsonResponseType> res = 
        restTemplate.exchange(url, HttpMethod.POST, entity, JsonResponseType.class);

System.out.println("Hello....." + res);

im getting 401 unauthorized error can some one remove it

also use

MultiValueMap<String, String> body = 
        new LinkedMultiValueMap<String, String>();

body.add("x-api-key", "myapikey");
body.add("x-api-secret", "myapisecret");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "application/json");
headers.set("Content-type", "application/json");

HttpEntity<?> entity = new HttpEntity<Object>(body, headers);
entity.getHeaders().setContentType(MediaType.APPLICATION_JSON);
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new
        ArrayList<HttpMessageConverter<?>>();

messageConverters.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
ResponseEntity<JsonResponseType> response =
        restTemplate.exchange(
                url, HttpMethod.POST, entity, JsonResponseType.class);

System.out.println("Hello....." + response);

this also have error 401 unauthorized not found

i am also use

HttpMethod.POST, requestEntity, YourResponseType.class);
System.out.println("Hello....." + responseEntity);`

RestTemplate restTemplate = new RestTemplate();
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);

HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);

HttpEntity<String> requestEntity = new HttpEntity<String>(
        "X-Api-Key:myapikey X-Api-Secret:=my api secret",
        headers);
ResponseEntity<YourResponseType> responseEntity = restTemplate
        .exchange("https://api.assembla.com/v1/spaces.json",

this have also same error

First thing to try is to remove headers.add("Content-type", "application/json"); . Your actual content-type is application/x-www-form-urlencoded .

The following code works fine for me:

   RestTemplate restTemplate = new RestTemplate();
   MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
   body.add("client_id", "aViwaUZXir44tcdmr6bg7m");
   body.add("client_secret", "65d952744a49774bcf24bcd32c521619");
   body.add("grant_type", "client_credentials");
   HttpHeaders headers = new HttpHeaders();
   headers.add("Accept", "application/json");
   HttpEntity<?> entity = new HttpEntity<Object>(body, headers);
   ResponseEntity<String> res = restTemplate.exchange(
       "https://api.assembla.com/token?", HttpMethod.POST, entity, String.class);

I'm even leaving a valid credentials here, as they are created solely for testing .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM